Regexp Expressions
In Bach, a regexp is a function that takes a Str and returns either null
or an object with at least a property start with type Num, and 0 with
type Str.
Regexp expressions denote such functions. They have the syntax of re2 regular
expressions, except for \C,
delimited by ~. They search the input string for a substring that matches the
regular expression. If none can be found, null is returned. Otherwise, an
object is returned with start indicating the offset (in codepoints) of the
first occurrence, and 0 containing the matched substring. If the regexp
contains capturing groups, the substrings matched by the groups are included as
additional properties – by group number and, in the case of named groups,
additionally by name.
| Program | Type | Value | Error |
|---|---|---|---|
"abccd" ~b(?P<cs>c*)d~ | Null|Obj<start: Num, 0: Str, 1: Null|Str, cs: Null|Str, Void> | {start: 1, 0: "bccd", 1: "cc", cs: "cc"} | |
"def" ~^b(?P<cs>c*)d~ | Null|Obj<start: Num, 0: Str, 1: Null|Str, cs: Null|Str, Void> | null | |
"abccd" ~^b(?P<cs>*)d~ | Syntax error |
Regexp expressions can be used with regexp funcers for greater effect.