下面的模式花了我很长时间才找到。当我最终找到它时,发现它在Python中不起作用。有谁知道是否有替代方案?
(*COMMIT)
定义:如果模式的其余部分不匹配,则导致整个匹配彻底失败。
(*FAIL)
在 Python 中也不起作用。但这可以用 (?!)
代替。
+-----------------------------------------------+
|Pattern: |
|^.*park(*COMMIT)(*FAIL)|dog |
+-------------------------------------+---------+
|Subject | Matches |
+-----------------------------------------------+
|The dog and the man play in the park.| FALSE |
|Man I love that dog! | TRUE |
|I'm dog tired | TRUE |
|The dog park is no place for man. | FALSE |
|park next to this dog's man. | FALSE |
+-------------------------------------+---------+
示例取自: regex match substring unless another substring matches
最佳答案
这可能不是通用替代品,但对于您的情况,您可以使用前瞻,断言狗匹配,但公园不匹配:^(?=.*dog)(?!.*park ).*$
您的 sample regex101
关于python - Python 的 PCRE 正则表达式 (*COMMIT) 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796252/