There are currently 2 forms of regex literals, both for re2.
While re2 does not have backreferences and some other features common to regexes, it guarantees to run in a time directly correlated with the size of the input. Common regexes do not do that.
re/pattern/ | re2 literal with interpretation of langur escape codes (first) |
RE/pattern/ | re2 literal without interpretation of langur escape codes |
As with string literals, the lowercase forms, such as re// interpret langur escape codes and the uppercase forms, such as RE//, do not.
Valid quote mark pairs are the same as for strings. Also the same are interpolation and langur escape codes.
Regex literals can be passed to regex functions, such as match() and replace().
Re2 modifiers available in langur are s, m, i, and U.
Modifiers are separated from the re or RE token, and from each other, with a colon.
re:i/abc/ # case insensitive search for "abc"
match("a\nC\n", by=re:s:m:U:i/a.c.*$/) == "a\nC"
Langur adds the x modifier for free-spacing mode. This is not a normal part of re2 regex so far. In free-spacing mode, you can also use line comments, using a # mark.
val r = re:x:block END matchme # comment in free-spacing regex # free-spacing not b/c of blockquote, but b/c of x modifier END
You can use an any modifier to allow any code point in a string or regex literal.
An esc modifier may be used on an re2 literal to automatically escape all interpolation strings (instead of specifying it on each interpolated value).
Using a block modifier after an re or RE token, separated by a colon, allows you to use block quotes with a specific marker. A block modifier is always the last modifier on a literal.
Note that this does not make a regex pattern "free-spacing." Use the free-spacing modifier for that.
The ending marker must be on a line of its own, with no trailing spaces. Leading spaces and tabs are allowed.
val re = re:block BLOCK_STRING some multi-line regex ... BLOCK_STRING
The line return after the opening marker and the line return before the ending marker are not part of the regex pattern.
The escape codes of langur will not always match the escape codes in a variety of regex. For example, the \P code represents a Unicode paragraph separator to langur, but a negated property class to re2 and other regexes. This is not a conflict, since they are not interpreted together.
Using the lowercase forms, langur escape codes will be interpreteat re/\\P{Lu}/ and RE/\P{Lu}/ will produce the same regex.
Using an esc modifier on an interpolation, such as re/{{x:esc}}/ indicates that you want to escape metacharacters.
You can also use the reEsc() function for re2.
The regex matching functions (match(), submatch(), replace(), etc.) understand all regex types available in langur.
For a value to test, these functions accept anything and convert it to a string if necessary
A regex as right-hand operand with a forward operator in a switch expression is used to test a value against the regex.
Non-string values are converted to strings
switch x, y { case -> re/abc+/: ... # either match re2 regex pattern abc+ case _, -> re/zzz/: ... # y matches re2 regex pattern zzz }
switch -> re/a+/ { case "abcd": ... # "abcd" -> re/a+/ # "abcd" matches re2 pattern re/a+/ case re/zzz/: ... # re/zzz/ -> re/a+/ case re/zzz/ == : ... # re/zzz/ == re/a+/ }