0

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?

Bob Polis
  • 23
  • 3

1 Answers1

4

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+>/

Davy Landman
  • 15,109
  • 6
  • 49
  • 73
  • 1
    To supply a working example: with `str text = "hello how help home";` followed by `println([match | /\b/ := text]);`, you will indeed get a list of matches: `["hello","how","help","home"]`. Thanks! – Bob Polis Dec 08 '15 at 11:18