string.regexp.subexpcount
Returns how many sub-expressions the pattern has.
Return value
The number of capturing groups in the pattern, plus one for the whole match: 1 when the pattern has no capturing group, one more for each (...) group written in it. (?:...) groups are not capturing and do not count. This is a static property of the compiled pattern - it needs no string to match against, and is the same before and after any call to match, search or capture.
Notes
subexpcount() always equals capture(str).len() once str matches.
Example
from "string" import regexp
println("regexp(\"abc\").subexpcount() =", regexp("abc").subexpcount()) // no groups: just the whole match
println("regexp(\"(?:a)(?:b)\").subexpcount() =", regexp("(?:a)(?:b)").subexpcount()) // non-capturing groups don't count
println("regexp(\"(a)(b)\").subexpcount() =", regexp("(a)(b)").subexpcount()) // two captures + the whole match
// it always equals capture(str).len() once str matches
let r = regexp("(a)(b)")
println("r.subexpcount() == r.capture(\"ab\").len() =", r.subexpcount() == r.capture("ab").len())Output:
regexp("abc").subexpcount() = 1
regexp("(?:a)(?:b)").subexpcount() = 1
regexp("(a)(b)").subexpcount() = 3
r.subexpcount() == r.capture("ab").len() = trueSee also
capture | Finds the first match of the pattern in str at or after start, and returns the whole match plus every capturing group as an array of spans. |
| regexp | class index |