Next: match_template
Prev: match_pattern
match_regexp(regexp, string, [case_matters])This function matches the regular expression regexp, a string,against the string string. If case_matters is specified and is true, the match is case-sensitive; otherwise, it is case-insensitive. If the match succeeds,
match_regexp() returns
a ten-element list giving the substitutions for the match (see below);
otherwise, match_regexp() returns 0.
Coldmud uses a regular expression matcher written by Henry Spencer. Its
syntax is very similar to the regular expression syntax used by Unix
utilities like ed or egrep. Here is Spencer's description
of his regular expression syntax:
A regular expression is zero or more branches, separated by |. It matches anything that matches one of the branches.The substitutions are the text in string which matches the parenthesized subexpressions in regexp. The first substitution is the text in string which matches the whole regexp. Thus, a regular expression can contain no more than nine parenthesized subexpressions. Substitutions are returned as two-element lists
A branch is zero or more pieces, concatenated. It matches a match for the first, followed by a match for the second, etc.
A piece is an atom possibly followed by *, +, or ?. An atom followed by * matches a sequence of 0 or more matches of the atom. An atom followed by + matches a sequence of 1 or more matches of the atom. An atom followed by ? matches a match of the atom, or the null string.
An atom is a regular expression in parentheses (matching a match for the regular expression), a range (see below), . (matching any single character), ^ (matching the null string at the beginning of the input string), $ (matching the null string at the end of the input string), a \ followed by a single character (matching that character), or a single character with no other significance (matching that character).
A range is a sequence of characters enclosed in []. It normally matches any single character from the sequence. If the sequence begins with ^, it matches any single character not from the rest of the sequence. If two characters in the sequence are separated by -, this is shorthand for the full list of ASCII characters between them (e.g.
[0-9]matches any decimal digit). To include a literal ] in the sequence, make it the first character (following a possible ^). To include a literal -, make it the first or last character.
[start, len] giving the index of the matching text in
string and the length of the text. When the substitutions are
ambiguous, leftmost * matches are always as long as possible.
If regexp is not a valid regular expression, match_regexp()
throws a ~regexp error.
Examples:
match_regexp("bar", "fooBAR")
=> [[4, 3], [0, 0], [0, 0], [0, 0], [0, 0],
[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
match_regexp("^([^ ]+) says, \"(.*)\"$", "Greg says, \"Hello.\"")
=> [[1, 19], [1, 4], [13, 6], [0, 0], [0, 0],
[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
match_regexp("[0-9]+", " 300 100 200 ")
=> [[2, 3], [0, 0], [0, 0], [0, 0], [0, 0],
[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
match_regexp("foo", "bar")
=> 0
match_regexp("foo", "Foo", 1)
=> 0