split one line regex in a multiline regexp in perl

前端 未结 1 751
渐次进展
渐次进展 2021-01-28 22:10

I have trouble spliting my regex in multiple line. I want my regex to match the line given:

* Code \"l;k\"\"dfsakd;.*[])_lkaDald\"

So I created

相关标签:
1条回答
  • 2021-01-28 22:35

    What the x flag does is very simply say 'ignore whitespace'.

    So you no longer match 'space' characters , and instead have to use \s or similar.

    So you can write:

    if ( m/
            ^
            \d+\s+
            fish:\w+\s+
            $ 
          /x ) {
        print "Matched\n";
    }
    

    You can test regular expressions with various websites but one example is https://regex101.com/

    So to take your example: https://regex101.com/r/eG5jY8/1

    But how is yours not working?

    This matches:

    my $string = q{* Code "l;k""dfsakd;.*[])_lkaDald"};
    
    my $firstRegexpr = qr/^\s*
                            \*
                           \s*
                           Code\s+
                           \"
                           (?<Code>((\")*[^\"]+)+)
                           \"
                        /x;
    
    print "Compiled_Regex: $firstRegexpr\n";
    print "Matched\n" if ( $string =~ m/$firstRegexpr/ );
    

    And as for not having $] - there's two answers. Either: Use \ to escape it, or use \Q\E.

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