正则表达式:否定整个单词,而不是1个字符

时间:2018-09-05 17:37:26

标签: python regex python-3.x

如果我这样做[^>],它将否定看起来像>的字符。

我如何否定整个单词?

即我想做类似[^people]的事情来忽略整个人吗?

编辑#1:

更好的例子

   a bunch of other text before this <property datatype = ""system.string"" description=""the sql command to be executed."" name=""sqlcommand""UITypeEditor="">select col_1, col_2, from dbo.table</property> a bunch of other text after this

现在,它仅返回以下内容:

<property datatype = ""system.string"" description=""the sql command to be executed."" name=""sqlcommand

但我希望它返回此值:

<property datatype = ""system.string"" description=""the sql command to be executed."" name=""sqlcommand""UITypeEditor="">select col_1, col_2, from dbo.table</property>

1 个答案:

答案 0 :(得分:2)

您不能使用字符类([])否定一个单词,需要查看零宽度环顾


首先,先行

要确保people立即遵循当前令牌:

(?!people)

要确保people之后在任何地方都没有遵循当前令牌:

(?!.*people)

现在,往后看:

要确保people不会在当前令牌之前立即

(?<!people)

Python的re模块不支持可变长度的向后查找,因此以下内容可确保people在当前标记位置之前没有出现在任何地方,不适用于re

(?<!.*people)

但适用于第三方regex模块。


这里所有模式都是零宽度的,即它们不消耗匹配中的字符,只需实现逻辑即可。