Regexp Module
The regexp module provides regular expression matching, searching, and
replacement using Go's regexp syntax. Import it with the regexp. prefix.
Functions
regexp.compile(pattern)
Compiles a regular expression pattern and returns a compiled regexp object. Use this when you need to reuse the same pattern multiple times for better performance.
let re = regexp.compile(`\d+`)regexp.match(pattern, str)
Returns true if the string matches the pattern, false otherwise.
Note: Due to match being a reserved keyword in Risor, this function cannot be
called directly via dot notation. Use regexp.find() and check if the result is not
nil as an alternative.
// Alternative: use find() and check for nil
regexp.find(`\d+`, "abc123") != nil // true (finds a match)
regexp.find(`^\d+$`, "abc123") != nil // false (no full-string match)
regexp.find(`^\d+$`, "12345") != nil // true (full-string match)regexp.find(pattern, str)
Returns the first match of the pattern in the string, or nil if there is
no match.
regexp.find(`\d+`, "abc123def456") // "123"
regexp.find(`\d+`, "no digits") // nilregexp.find_all(pattern, str, n?)
Returns a list of all matches of the pattern in the string. The optional n
parameter limits the number of matches returned. If n is omitted or
negative, all matches are returned.
regexp.find_all(`\w+`, "hello world") // ["hello", "world"]
regexp.find_all(`\d+`, "a1b2c3d4", 2) // ["1", "2"]
regexp.find_all(`[aeiou]`, "hello") // ["e", "o"]regexp.search(pattern, str)
Returns the index of the first match of the pattern in the string, or -1 if there is no match.
regexp.search(`\d+`, "abc123") // 3
regexp.search(`\d+`, "abcdef") // -1regexp.replace(pattern, str, repl, count?)
Replaces matches of the pattern in the string with the replacement. The
optional count parameter limits the number of replacements. If count is
omitted, all matches are replaced.
regexp.replace(`\d`, "a1b2c3", "X") // "aXbXcX"
regexp.replace(`\d`, "a1b2c3", "X", 2) // "aXbXc3"
regexp.replace(`\s+`, "a b c", " ") // "a b c"regexp.split(pattern, str, n?)
Splits the string by occurrences of the pattern. The optional n parameter
limits the number of substrings returned. If n is omitted or negative, there
is no limit.
regexp.split(`\s+`, "one two three") // ["one", "two", "three"]
regexp.split(`,\s*`, "a, b, c") // ["a", "b", "c"]
regexp.split(`\s+`, "one two three", 2) // ["one", "two three"]regexp.escape(str)
Escapes all regular expression metacharacters in the string so that the result can be used as a literal pattern.
regexp.escape("price: $10.00") // "price: \\$10\\.00"Pattern Syntax
Risor uses Go's regular expression syntax, which is based on RE2. Some highlights:
| Pattern | Meaning |
|---|---|
. | Any character except newline |
\d | Digit [0-9] |
\w | Word character [a-zA-Z0-9_] |
\s | Whitespace character |
* | Zero or more of the preceding |
+ | One or more of the preceding |
? | Zero or one of the preceding |
^ | Start of string |
$ | End of string |
(...) | Capture group |
[...] | Character class |
| | Alternation |
For the full syntax reference, see the Go regexp documentation.