How do I access captured substrings after a successful regex match in Perl?

前端 未结 4 1715
余生分开走
余生分开走 2021-01-24 11:09

I am searching for a string in Perl and storing it in another scalar variable. I want to print this scalar variable. The code below doesn\'t seem to work. I am not sure what is

4条回答
  •  花落未央
    2021-01-24 11:46

    The regex matching operation returns true (1) for match success, false otherwise. If you want to retrieve the match, you should try one of the following:

    • use the match variables $1, $2...
    • match in list context ($m1, $m2) = $string =~ /$regex/

    Note that you need to use captures in your regex for these to work. Which you're not doing yet.

    You ought to take a look at the complete documentation in perlop, section "Regexp Quote-Like Operators"

提交回复
热议问题