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.
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.