Perl Named Captured Group

前端 未结 1 903
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 02:34

I have created two named capture variables in the regex and the second one doesn\'t seem to return any value while the first one can. I am not sure why..here is the code.

<
相关标签:
1条回答
  • 2021-01-28 03:02

    You probably mean:

    my $string = 'test [google] another test [windows]';
    
    if( $string =~ /.*?\[(?<firstBracket>\w+)\].*?\[(?<secondBracket>\w+)\]/i ) {
            say $+{firstBracket};
            say $+{secondBracket};
    }
    

    output

    google
    windows
    

    or

    my $re = qr/.*?\[(?<firstBracket>\w+)\].*?\[(?<secondBracket>\w+)\]/i;
    if( $string =~ $re ) {
        ...
    }
    

    with same output...

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