How can I match a pipe character followed by whitespace and another pipe?

前端 未结 5 1574
春和景丽
春和景丽 2021-01-12 01:14

I am trying to find all matches in a string that begins with | |.

I have tried: if ($line =~ m/^\\\\\\|\\s\\\\\\|/) which didn\'t work. <

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 01:46

    Pipe character should be escaped with a single backslash in a Perl regex. (Perl regexes are a bit different from POSIX regexes. If you're using this in, say, grep, things would be a bit different.) If you're specifically looking for a space between them, then use an unescaped space. They're perfectly acceptable in a Perl regex. Here's a brief test program:

    my @lines = ;
    
    for (@lines) {
        print if /^\| \|/;
    }
    
    __DATA__  
    | | Good - space  
    || Bad - no space  
    |   | Bad - tab  
     | | Bad - beginning space  
            Bad - no bars  
    

提交回复
热议问题