匹配如果单词没有出现在正则表达式中的另一个单词之前

时间:2015-07-07 13:53:28

标签: python regex

我正在尝试编写匹配的正则表达式,如果关键字在停用词之前没有出现在字符串中的某个位置。

示例:

这不应该匹配任何东西:

  

aaa:好的

     

bbb:失败

     

ccc:好的

     

停止字

     

随机文本确定随机文本失败

这应该匹配:

  

aaa:好的

     

bbb:好的

     

ccc:好的

     

停止字

     

随机文本确定随机文本失败

我想我应该使用否定支票^(?!.*Failed),但如何停止[stopword]检查?

如果重要的话,我正在尝试使用pythons regex引擎。

编辑:抱歉不清楚。是的,它是“失败”我想不匹配,不幸的是我需要一个“正则表达式”-solution即re.search(regex, string),因为我没有任何访问输入变量regex和string之外的代码。

如果我可以直接在Python中完成它,我会避免使用正则表达式。 ;)

2 个答案:

答案 0 :(得分:3)

如果我理解正确,您需要确保字符串'Failed'不会出现在字符串'stopword'之前的任何位置。假设是这种情况,最简单的方法是在Failed之前找出stopword,然后反转您的结果。假设text是您的输入:

not re.match(r'.*?Failed.*?stopword', text, re.DOTALL)

当然,使用直接python会更容易:

'Failed' not in text.split('stopword')[0]

答案 1 :(得分:0)

无论你想要实现什么,你都不会很好地解释它。

如果要检查字符串中是否存在“失败”,而不是在特定子字符串之后检查,则将字符串拆分并仅查看第一部分。

>>> sample1 = """
... aaa: OK
... bbb: Failed
... ccc: OK
... stopword
... Random Text OK Failed Random Text
... """
>>> sample2 = """
... aaa: OK
... bbb: OK
... ccc: OK
... stopword
... Random Text OK Failed Random Text
... """
>>>
>>> print "Failed in sample1:", 'Failed' in sample1.partition('stopword')[0]
Failed in sample1: True
>>> print "Failed in sample2:", 'Failed' in sample2.partition('stopword')[0]
Failed in sample2: False
>>>
相关问题