JMT*_*ler 5 regex escaping negative-lookbehind
我正在尝试编写一个匹配所有内容的正则表达式但是没有被转义的撇号.考虑以下:
<?php $s = 'Hi everyone, we\'re ready now.'; ?>
Run Code Online (Sandbox Code Playgroud)
我的目标是编写一个基本匹配字符串部分的正则表达式.我在考虑像
/.*'([^']).*/
Run Code Online (Sandbox Code Playgroud)
为了匹配一个简单的字符串,但我一直试图弄清楚如何在撇号上得到负面的lookbehind,以确保它没有反斜杠......
有任何想法吗?
- JMT
<?php
$backslash = '\\';
$pattern = <<< PATTERN
#(["'])(?:{$backslash}{$backslash}?+.)*?{$backslash}1#
PATTERN;
foreach(array(
"<?php \$s = 'Hi everyone, we\\'re ready now.'; ?>",
'<?php $s = "Hi everyone, we\\"re ready now."; ?>',
"xyz'a\\'bc\\d'123",
"x = 'My string ends with with a backslash\\\\';"
) as $subject) {
preg_match($pattern, $subject, $matches);
echo $subject , ' => ', $matches[0], "\n\n";
}
Run Code Online (Sandbox Code Playgroud)
印刷
<?php $s = 'Hi everyone, we\'re ready now.'; ?> => 'Hi everyone, we\'re ready now.'
<?php $s = "Hi everyone, we\"re ready now."; ?> => "Hi everyone, we\"re ready now."
xyz'a\'bc\d'123 => 'a\'bc\d'
x = 'My string ends with with a backslash\\'; => 'My string ends with with a backslash\\'
Run Code Online (Sandbox Code Playgroud)