如何在括号中提取不以特定关键字开头的字符串(断言后面有负面看法?)

tig*_*ero 0 python regex negative-lookbehind

我想从下面的字符串中找到(test)字符串的字符串位置(既不以for或or开头):

 •  <test>* (for test) (or test) (test)
Run Code Online (Sandbox Code Playgroud)

是否可以使用断言后面的负面查找来找到特定的字符串?我正在使用这个正则表达式,但我错过了一些东西:

m_comments = re.search('(?<!\(for)|(?<!\(or)', line)
Run Code Online (Sandbox Code Playgroud)

还可以在断言后面结合使用或声明吗?

注意:test可以是任何可能的字符串:

<an other eg> (for another test) (or with this) (anything)
Run Code Online (Sandbox Code Playgroud)

ste*_*ema 5

试试这个

\((?!for|or).*?\)
Run Code Online (Sandbox Code Playgroud)

在Regexr上看到它

随着\(.*?\)我匹配从开口支架到第一个闭合支架的所有东西.

然后我使用负向前瞻(?!for|or)以确保在开始括号之后没有"for"和"or".

在Python中的lookbehind断言中,不可能使用替换.它们必须是固定长度的.