Which regular expression operator means 'Don't' match this character?

*, ?, + characters all mean match this character. Which character means 'don't' match this? Examples would help.

302184 次浏览

^用于字符范围的开头,或负向前/后看断言。

>>> re.match('[^f]', 'foo')
>>> re.match('[^f]', 'bar')
<_sre.SRE_Match object at 0x7f8b102ad6b0>
>>> re.match('(?!foo)...', 'foo')
>>> re.match('(?!foo)...', 'bar')
<_sre.SRE_Match object at 0x7f8b0fe70780>

说“不匹配”有两种方式: 字符范围和零宽度负向前/后视。

The former: don't match a, b, c or 0: [^a-c0]

后者: 匹配除 foobar之外的任何三个字母的字符串:

(?!foo|bar).{3}

或者

.{3}(?<!foo|bar)

另外,一个更正为您: *?+实际上不匹配任何东西。它们是重复操作符,并且始终遵循匹配操作符。因此,a+意味着匹配一个或多个 a[a-c0]+意味着匹配一个或多个 abc0,而 ?0将匹配一个或多个任何不是 abc0

[^](在 [ ]内)在正则表达式中是否,而 ^是“字符串的开始”

[^a-z]匹配从“ a”到“ z”的任何单个字符 没有

^[a-z]表示字符串从“ a”开始到“ z”

参考文献

您可以使用否定字符类来排除某些字符: 例如,[^abcde]将匹配除 a、 b、 c、 d、 e 字符之外的任何字符。

Instead of specifying all the characters literally, you can use shorthands inside character classes: [\w] (lowercase) will match any "word character" (letter, numbers and underscore), [\W] (uppercase) will match anything but word characters; similarly, [\d] will match the 0-9 digits while [\D] matches anything but the 0-9 digits, and so on.

如果你使用 PHP,你可以看看 正则表达式字符类文档