JanetDocsSourcePlaygroundI'm feeling luckyCommunityGitHub sign in

peg/match

core-api


    cfunction
    src/core/peg.c on line 1904, column 1

    (peg/match peg text &opt start & args)

    Match a Parsing Expression Grammar to a byte string and return an 
    array of captured values. Returns nil if text does not match the 
    language defined by peg. The syntax of PEGs is documented on the 
    Janet website.


3 examplesSign in to add an example
Loading...
# convert a string to an integer
(peg/match '(number :d+) "123")
#=> @[123]

(first (peg/match '(number :d+) "123"))
#=> 123
erichaneyPlayground
# more at https://github.com/sogaiu/margaret/tree/master/examples
(peg/match ~{:main (replace (some :header-line)
                            ,(fn [& captures]
                               (table ;captures)))
             :header-line (sequence (capture :header-name) ":"
                                    :s+
                                    (capture :header-value) :crlf)
             :header-name (to ":")
             :header-value (to :crlf)
             :crlf "\r\n"}
           (string "Content-Type: text/plain\r\n"
                   "Content-Length: 1024\r\n"))
# => @[@{"Content-Length" "1024" "Content-Type" "text/plain"}]
sogaiuPlayground
(peg/match ~{:main (capture (some :S))}
           "hello world")
# => @["hello"]
sogaiuPlayground