Takkun
Takkun

Reputation: 6361

Regex - Match all occurrences?

my @matches = ($result =~ m/INFO\n(.*?)\n/);

So in Perl I want to store all matches to that regular expression. I'm looking to store the value between INFO\n and \n each time it occurs.

But I'm only getting the last occurrence stored. Is my regex wrong?

Upvotes: 11

Views: 37156

Answers (1)

Tim
Tim

Reputation: 14154

Use the /g modifier for global matching.

my @matches = ($result =~ m/INFO\n(.*?)\n/g);

Lazy quantification is unnecessary in this case as . doesn't match newlines. The following would give better performance:

my @matches = ($result =~ m/INFO\n(.*)\n/g);

/s can be used if you do want periods to match newlines. For more info about these modifiers, see perlre.

Upvotes: 15

Related Questions