Php 忽略字符双重外观的正则表达式模式
寻找一个正则表达式模式,该模式检测单个字符(特定),但在出现双字符或三字符时忽略它。。。NPhp 忽略字符双重外观的正则表达式模式,php,regex,preg-replace,Php,Regex,Preg Replace,寻找一个正则表达式模式,该模式检测单个字符(特定),但在出现双字符或三字符时忽略它。。。N abcde <- looking for this (c's separated by another character) abccdce <- not this (immediately repeating c's) 提示:我知道如何做相反的事情-替换双精度,但忽略单精度 $pattern = '/c\1{1}/x'; $replacement = 'FOO'; preg_replace
abcde <- looking for this (c's separated by another character)
abccdce <- not this (immediately repeating c's)
提示:我知道如何做相反的事情-替换双精度,但忽略单精度
$pattern = '/c\1{1}/x';
$replacement = 'FOO';
preg_replace($pattern, $replacement, $text);
您可以为此使用:
(?<!c)c(?!c)
代码:
(?<!c) # negative lookbehind to fail the match if previous position as c
c # match literal c
(?!c) # negative lookahead to fail the match if next position as c
$repl = preg_replace('/(?<!c)c(?!c)/', 'FOO', $text);
$repl=preg_replace('/(?为什么“abccdce”不产生“abccdFOOe”?)作为regex模式中的旁注{1}
总是没有用的。
$repl = preg_replace('/(?<!c)c(?!c)/', 'FOO', $text);