- 论坛徽章:
- 0
|
In Perl regular expressions, you can surround a subexpression with \Q and \E to indicate that you want that subexpression to be matched as a literal string even if there are metacharacters in there. You also have the quotemeta function that inserts exactly the right number of backslashes in a string so that if you subsequently interpolate that string into a regular expression, it will be matched literally, no matter what its contents were!- #!/usr/bin/perl -w
- use strict;
- use warnings;
- my $a="abc[0]";
- my $b="bc[0]";
- my $c=quotemeta($b); # $c eq ‘bc\[0\]’;
- if($a=~/$c/){print "match\n";} # results in “match” being printed.
- if($a=~/\Q$b\E/){print "match\n";} # results in “match” being printed.
复制代码 |
|