我需要區分具有單反斜槓和雙反斜槓的字符串。 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
即它匹配雙反斜線單之一。
能否請你幫我的正則表達式匹配雙而不是單反斜線?
嘗試:'/^\\(?:[^ \\] | $)/' – shawnhcorey
謝謝!負面看法是我的問題的答案。 –