Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式,两个负前瞻语句_Python_Regex_Negative Lookahead - Fatal编程技术网

Python正则表达式,两个负前瞻语句

Python正则表达式,两个负前瞻语句,python,regex,negative-lookahead,Python,Regex,Negative Lookahead,我有一个经过语法分析的文本字符串: s = 'ROOT (S (VP (VP (VB the) (SBAR (S (NP (DT same) (NN lecturer)) (VP (VBZ says)' 我想把“相同”和s匹配起来。关键是“the”和“same”仅在由语法标记(即,(,NP,s等)分隔时匹配。因此,“same”不应在s2中找到匹配项: s2= 'ROOT (S (VP (VP (VB the) (SBAR (S (NP (DT lecturer) (NN same)) (V

我有一个经过语法分析的文本字符串:

 s = 'ROOT (S (VP (VP (VB the) (SBAR (S (NP (DT same) (NN lecturer)) (VP (VBZ says)'
我想把“相同”和s匹配起来。关键是“the”和“same”仅在由语法标记(即,(,NP,s等)分隔时匹配。因此,“same”不应在s2中找到匹配项:

 s2= 'ROOT (S (VP (VP (VB the) (SBAR (S (NP (DT lecturer) (NN same)) (VP (VBZ says)'
我尝试了一个双重否定的前瞻性断言,但没有成功:

 >>>rx = r'the(?![a-z]*)same(?![a-z]*)'
 >>>re.findall(rx,s)
 []
其思想是在不后跟小写字符时匹配“The”,然后在不后跟小写字符时匹配“same”


有谁有更好的方法吗?

因此,如果
相同的
之间的所有字符都不是小写字母,那么您需要进行匹配,以下是如何在正则表达式中编写它们:

the[^a-z]*same
请注意,您可能还希望添加单词边界,因此您不匹配类似于
foothe…samebar
,如下所示:

\bthe\b[^a-z]*\bsame\b

正则表达式不擅长解析嵌套结构。对于您的特定示例,它可能就足够了,因为您似乎并不关心嵌套。没错,嵌套结构不是问题所在。当尝试将“same”与执行该操作的字符串“theABCD123sameEFG456”匹配时,也会出现同样的问题。我实际上尝试过类似的方法起初,但我一定是在什么地方打错了什么。无论如何,谢谢!