我需要区分带有单反斜杠和双反斜杠的字符串。Perl 平等对待它们:
print "\n" . '\qqq\www\eee\rrr';
print "\n" . '\\qqq\www\eee\rrr';
将给出相同的结果:
\qqq\www\eee\rrr
\qqq\www\eee\rrr
更重要的是,下一个电话:
print "\n" . leadingBackSlash('\qqq\www\eee\rrr');
print "\n" . leadingBackSlash('\\qqq\www\eee\rrr');
print "\n" . leadingBackSlash('\\\qqq\www\eee\rrr');
print "\n" . leadingBackSlash('\\\\qqq\www\eee\rrr');
发挥作用:
sub leadingBackSlash {
$_ = shift;
print "\n$_";
print "\n" . length($_);
if( m/^\\\\/) {
print "\ndouble backslash is matched";
}
if( m/^\\/) {
print "\nsingle backslash is matched";
}
}
将产生结果:
\qqq\www\eee\rrr
16
single backslash is matched
\qqq\www\eee\rrr
16
single backslash is matched
\\qqq\www\eee\rrr
17
double backslash is matched
single backslash is matched
\\qqq\www\eee\rrr
17
double backslash is matched
single backslash is matched
即它将双反斜杠匹配为单个反斜杠。
你能帮我用正则表达式匹配双反斜杠而不是单反斜杠吗?