Reputation: 55
import re
str="Everyone loves Stack Overflow"
print(re.findall("[ESO][^.]",str))
I don't understand why [^.]
does anything. I thought it only matches characters that are not characters - in other words: nothing! But the output is the following:
['Ev', 'St', 'Ov']
Can someone shed some light on this? It's impossible to search for something like [^.]
on google, and pythondocs about regular expressions didn't help either.
Upvotes: 3
Views: 179
Reputation: 244807
Most characters lose their special meaning when they are inside a character group.
So .
matches any character, but [.]
matches only dot. Thus [^.]
matches everything that is not dot.
Upvotes: 3
Reputation: 8522
Character classes []
have their own little language. Specifically, the dot .
inside a character class matches the actual .
(and is not a wildcard).
Upvotes: 4
Reputation: 61379
Most of the regular expression special characters lose their special meaning within a character class (square brackets), so while .
matches any character, [.]
matches a literal .
and [^.]
matches any character other than .
. You will sometimes see people wrap a character like .
in square brackets just to make sure it's treated literally without having to worry about any corner cases in a regular expression library.
Upvotes: 15