-2

我需要找到以 @ 开头的条目,并且在此示例中的下一个 @ 之前没有单词“关键字”:

@something
something
something
keyword
something
something

@something
something
something
something
something

@something
something
something
keyword
something
something

所以这个搜索字符串会指向第二个例子。有人知道如何用正则表达式做到这一点吗?

4

1 回答 1

-1

以下正则表达式似乎正在工作:

@\w+(?![^@]*\bkeyword\b[^@]*)

演示

解释:

@\w+             match a @label
(?!              provided that we DON'T see ahead
    [^@]*        any text without another @label
    \bkeyword\b  which also has the "keyword"
)
于 2020-07-06T06:30:40.863 回答