Product Site

5.4.15.3. parse

Returns 0 after setting and successfully parsing the regular expression pattern. The new pattern will be used to match strings specified with methods match or pos. Returns an error code otherwise.

The type of regular expression matching can be set to “greedy” by specifying option MAXIMAL, or to “lazy” by specifying option MINIMAL. The default is to use the current matching type.

Return values:
0

Regular expression was parsed successfully.
1

An unexpected symbol was met during parsing.
2

A missing ')' was found.
3

An illegal set was defined.
4

The regular expression ended unexpectedly.
5

An illegal number was specified.
6

An undefined symbolic set name was specified.
Example 5.276. RegularExpression class — parse method
patterns = "A [:alpha:]{4} fl?*.", -
           "?*[l|e]?*e?*[r|g]?*", -
           "[invalid"
texts = "A nice flower.", -
        "A yellow flower.", -
        "A blue flag."

re = .RegularExpression~new
do pattern over patterns
  code = re~parse(pattern)
  if code == 0 then
    do text over texts
      say text~left(16) -
       re~match(text)~?("matches", "doesn't match") "regex" pattern
    end
  else
    say "error" code "parsing pattern" pattern
  say
end

::requires rxregexp.cls

Output:
A nice flower.   matches regex A [:alpha:]{4} fl?*.
A yellow flower. doesn't match regex A [:alpha:]{4} fl?*.
A blue flag.     matches regex A [:alpha:]{4} fl?*.

A nice flower.   matches regex ?*[l|e]?*e?*[r|g]?*
A yellow flower. matches regex ?*[l|e]?*e?*[r|g]?*
A blue flag.     matches regex ?*[l|e]?*e?*[r|g]?*

error 3 parsing pattern [invalid
Example 5.277. RegularExpression class — parse method
nrs = 1, 42, 0, 5436412, "1A", "f43g"
re = .RegularExpression~new("[1-9][0-9]*")
do nr over nrs
  say nr "is" re~match(nr)~?("a valid", "an invalid") "number"
end
say

-- allow hexadecimal numbers and a single 0
re~parse("0|([1-9a-fA-F][:xdigit:]*)")
do nr over nrs
  say nr "is" re~match(nr)~?("a valid", "an invalid") "number"
end

::requires rxregexp.cls

Output:
1 is a valid number
42 is a valid number
0 is an invalid number
5436412 is a valid number
1A is an invalid number
f43g is an invalid number

1 is a valid number
42 is a valid number
0 is a valid number
5436412 is a valid number
1A is a valid number
f43g is an invalid number