How can I store captures from a Perl regular expression into separate variables?

后端 未结 5 980
无人及你
无人及你 2020-12-15 06:01

I have a regex:

/abc(def)ghi(jkl)mno(pqr)/igs

How would I capture the results of each parentheses into 3 different variables, one for each

5条回答
  •  清歌不尽
    2020-12-15 06:59

    @OP, when parenthesis are captured, you can use the variables $1,$2....these are backreferences

    $string="zzzabcdefghijklmnopqrsssszzzabcdefghijklmnopqrssss";
    while ($string =~ /abc(def)ghi(jkl)mno(pqr)/isg) {
        print "$1 $2 $3\n";
    }
    

    output

    $ perl perl.pl
    def jkl pqr
    def jkl pqr
    

提交回复
热议问题