^ : anchor match to beginning of string
(?: : begin a non-capture group
(?!\bowner\b) : 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
\bdog\b : match 'dog' surrounded by word boundaries
您可以使用下面的正则表达式来验证字符串是否包含前面没有单词“owner”的单词“dog”
Start your engine!<“\(ツ) /'>;Python code
Python的正则表达式引擎执行以下操作
匹配不以非法单词开头的单个字符序列的技术称为Tempered Greedy Token Solution
另一个选项是开始匹配除
o
或换行符以外的任何字符然后,如果您遇到一个o,则断言它不是单词所有者,然后匹配除o或换行符之外的任何字符,并有选择地重复该过程,直到匹配单词dog为止
解释
^
字符串的开头[^o\r\n]*
匹配0+乘以除o或换行符以外的任何字符(?:
非捕获组(?!\bowner\b)
负向前看,不要直接在右边断言单词ownero[^o\r\n]*
匹配o,后跟0+乘以除o或换行符以外的任何字符)*
关闭非捕获组并重复0+次\bdog\b
匹配单词dogRegex demoPython demo
相关问题 更多 >
编程相关推荐