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?
Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch.
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
The match() regex method can also be used for checking the match which retrieves the result of matching a string against a regex: One of the differences between match() and test() methods is that the first works only with strings, the latter works also with integers.
The search() function searches the string for a match, and returns a Match object if there is a match.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With