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

前端 未结 5 1566
春和景丽
春和景丽 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:49

    If it's a literal string you're searching for, you don't need a regular expression.

    my $search_for = '| |';
    my $search_in = whatever();
    if ( substr( $search_in, 0, length $search_for ) eq $search_for ) {
        print "found '$search_for' at start of string.\n";
    }
    

    Or it might be clearer to do this:

    my $search_for = '| |';
    my $search_in = whatever();
    if ( 0 == index( $search_in, $search_for ) ) {
        print "found '$search_for' at start of string.\n";
    }
    

    You might also want to look at quotemeta when you want to use a literal in a regexp.

提交回复
热议问题