如何在一个负向回溯捕获词条正则表达式之间捕获未知数量的单词? [英] How to capture unknown number of words in between a negative lookbehind catch term regex?
本文介绍了如何在一个负向回溯捕获词条正则表达式之间捕获未知数量的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试排除单词&owner";在单词";og";之前的某个位置的记录
- 主人有一只狗(不包括)
- 主人有一只黑色和棕色的狗(不包括)
- John有一只狗(包括)
- John有一只黑色和棕色的狗(包括)
以下是当前正则表达式:
(?<!owners)w+sdog
这适用于单个未知词(不包括‘OwnerHasDog’,但包括‘OwnerHave aDog’)),但是,我无法捕获在&Owner和&Dog&之间的所有单词中保留其负面影响的多个单词。
非常感谢
推荐答案
您可以使用以下正则表达式来验证该字符串是否包含前面不带";Owner";的单词。
^(?:(?!owner).)*dog
Start your engine!<;¯(ツ)/¯>;Python code
Python的正则表达式引擎执行以下操作。
^ : anchor match to beginning of string
(?: : begin a non-capture group
(?!owner) : use a negative lookahead to assert that the current
position in the string is not followed by "owner"
. : match a character
) : end non-capture group
* : execute non-capture group 0+ times
dog : match 'dog' surrounded by word boundaries
匹配不以非法单词开头的单个字符序列的技术称为Tempered Greedy Token Solution。
这篇关于如何在一个负向回溯捕获词条正则表达式之间捕获未知数量的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文