I have a str
value containing a fair amount of text, and I match it against a regex. The str
contains multiple matches of the regex, but of course I only get the first.
How can I enumerate over the other matches, of better, how can I collect them into a list[str]
?
Example:
str text = "hello here how home";
Now I can do:
if (/<match:h+>/ := text) println(match);
which prints the first match: hello
.
Now, instead, I'd love to collect all matches into a list[str]
. Other languages provide the g
flag for global matching. What's Rascal's idiom for this?
In general you can iterate over a pattern until there are no new matches to be made.
for (/<match:h+>/ := "hello here how home") {
println(match);
}
or
[ match | /<match:h+>/ := "hello here how home"]
the reverse is also true, if you want to know if there is at least one item in a list:
if (_ <- lst) {
println("lst is not empty");
}
Read more on Pattern Matching and Enumerators in the tutor.
If you want to match the word you might have to change the regex: /<word:h\w+>/