Which regex operator does not match this character?

*, ?, + characters mean coincidence with this character. What symbol does not match mean? Examples will help.

+62
regex
May 08 '11 at 5:13 a.m.
source share
4 answers

You can use negative character classes to exclude certain characters: for example, [^abcde] will match everything except the characters a, b, c, d, e.

Instead of specifying all characters literally, you can use abbreviated inner character classes: [\w] (lowercase) will match any "word character" (letter, numbers and underscore), [\w] (uppercase) will match any the word, except for the characters of the word; likewise, [\d] will match digits 0-9, and [\d] matches anything except digits 0-9, etc.

If you use PHP, you can take a look at the documentation of regex character classes .

+58
May 08 '11 at 5:22 a.m.
source share

There are two ways to say "do not match": character ranges and negative inverse mappings with zero width / lookbehind.

First: do not match a , b , c or 0 : [^a-c0]

Last: matches any three-letter string except foo and bar :

(?!foo|bar).{3}

or

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

In addition, an amendment for you: * ? and + actually doesn't match anything. They are repetition operators and always follow the corresponding operator. Thus, a+ means the coincidence of one or more of a , [a-c0]+ means the coincidence of one or more of a , b , c or 0 , while [^a-c0]+ will correspond to one or more of all that there was no a , b , c or 0 .

+51
May 08 '11 at 5:16
source share

[^] (inside [] ) is the negation in the regular expression, while ^ is the "start of line"

[^az] matches any single character not from "a" to "z"

^[az] means the line starts from "a" to "z"

Link

+30
May 08 '11 at 5:21
source share

^ used at the beginning of a range of characters, or negative statements lookahead / lookbehind.

 >>> 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> 
+6
May 08 '11 at 5:16 a.m.
source share



All Articles