How can I extract the matches from the Perl match operator into variables?

前端 未结 6 1045
无人及你
无人及你 2020-12-29 08:09

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?

6条回答
  •  清酒与你
    2020-12-29 08:10

    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;
    

提交回复
热议问题