How can I do a global regular expression match in Perl?

前端 未结 2 1225
暗喜
暗喜 2020-12-22 01:41

I am trying to come up with a regular expression in Perl matching multiple patterns and returning all of them like preg_match_all in PHP does.

Here\'s w

相关标签:
2条回答
  • 2020-12-22 02:18

    The Perl equivalent to

    preg_match_all('/(test|data|string)/', 'testdatastring', $m)
    

    is

    @m = ("testdatastring" =~ m/(test|data|string)/g);
    

    The /g flag stands for global, so it returns a list of matches in list context.

    0 讨论(0)
  • 2020-12-22 02:28

    See http://www.anaesthetist.com/mnm/perl/regex.htm . Basic syntax (from there) is:

    $_ = "alpha xbetay xgammay xdeltay so on";
    ($first, $second, $third) = /x(.+?)y/g;
    

    Note the /g

    0 讨论(0)
提交回复
热议问题