If I have a match operator, how do I save the parts of the strings captured in the parentheses in variables instead of using $1
, $2
, and so on?
... = m/stuff (.*) stuff/;
What goes on the left?
The trick is to make m// work in list context by using a list assignment:
($interesting) = $string =~ m/(interesting)/g;
This can be neatly extended to grab more things, eg:
($interesting, $alsogood) = $string =~ m/(interesting) boring (alsogood)/g;
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