正则表达式匹配没有给定前缀的特定字符串

Gui*_*ume 51 regex

我需要匹配包含值且没有给定前缀的所有行.

示例:我想要所有包含word前缀的行prefix

所以:

foobar -> no match
prefix word -> no match
prefix word suffix -> no match
word -> MATCH
something word -> MATCH
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的:

(?!prefix)word
Run Code Online (Sandbox Code Playgroud)

似乎没有做我想要的

How*_*ard 78

你可能需要

(?<!prefix )word
Run Code Online (Sandbox Code Playgroud)

(也许照顾空间).

(?!)是一个负面的前瞻,但在你的情况下,你需要一个负面的lookbehind(即(?<!)).

  • 这将错误地匹配"前缀另一个单词"这一行.问题指出该行以前缀开头并包含"word",但没有说"word"总是紧跟"prefix". (3认同)