【发布时间】:2020-02-09 10:10:07
【问题描述】:
以这个字符串为例:
focus;focus#focus#focus 焦点
如果我想查找子字符串focus,我需要这个结果(粗体匹配):
focus;focus#focus#focus focus
据我所知:(?<!#)(focus)(?!#),演示here。
这种模式显然不起作用,因为排除了 # 旁边的子字符串,我也需要包含这些子字符串。
从编程上讲,这就是我需要的:
if !(prefix == "#" && suffix == "#") {
// Is a match
}
【问题讨论】:
-
在lookbehind中放一个lookahead:
(?<!#(?=focus#))或使用众所周知的回溯控制动词:#focus#(*SKIP)(*F)|focus -
避免在正则表达式问题中使用与语言无关的标签(即使使用 pcre 或其他引擎标签),因为实现之间支持的功能可能不同。
标签: regex language-agnostic pcre