string.regexp.match
Returns whether the pattern matches the whole of str.
Parameters
str | string | the string to test |
Return value
true when the pattern matches all of str, from the first character to the last. false for an ordinary mismatch. false never means the match was aborted, see Errors.
Errors
Throws regexp match aborted: pattern too complex for this input instead of returning false when matching exceeds the engine's backtracking budget - for example a pattern with nested unbounded quantifiers tried against a long non-matching input. It is a runtime safety limit, not a syntax error, so it can happen on any pattern given a bad enough input.
Notes
match is anchored at both ends. Use search to test whether the pattern occurs anywhere inside str, and capture to get its capturing groups too.
Example
from "string" import regexp
let digits = regexp("[0-9]+")
println("digits.match(\"12345\") =", digits.match("12345")) // the whole string has to match
println("digits.match(\"12345 items\") =", digits.match("12345 items")) // trailing text fails the match
// a pattern that forces runaway backtracking throws rather than hanging
local s = ""
for (local i = 0; i < 26; i++) s += "a"
try {
regexp("(a+)+b").match(s)
} catch (e) {
println("regexp(\"(a+)+b\").match(s) throws:", e)
}Output:
digits.match("12345") = true
digits.match("12345 items") = false
regexp("(a+)+b").match(s) throws: regexp match aborted: pattern too complex for this inputSee also
search | Finds the first match of the pattern in str at or after start, and returns its span. |
constructor | Compiles pattern into a reusable regexp instance. |
| regexp | class index |