String.matchAll
matchAll($regexPattern, $groupNames='') -> list
Description
Find all matches for the given regex pattern and return a List.
If there are no matches, it returns an empty List []
.
Each match value behaves like the match
method. If there are capture groups defined by parens (...)
, it will be a Map of captured values. Otherwise it will be a string of the entire match.
'red, green, blue'.matchAll(rx'\w+') //= ['red', 'green', 'blue'] 'abc def'.matchAll(rx'\d+') //= [] 'a:123, z:789'.matchAll(rx'(\w):(\d+)', 'letter|number') // [ // { // full: 'a:123' // letter: 'a' // number: '123' // } // { // full: 'z:789' // letter: 'z' // number: '789' // } // ]
Using matchAll
with if
When you call this method in an if
statement, you can use the If-Assign :=
operator to get the returned result and use it inside the block.
$str = 'red, green, blue' if $colors := $str.matchAll(rx'\w+') { print($colors) //= ['red', 'green', 'blue'] }