Entry
Why do the patterns ^[ \t] and ^[^ \t]* get the same result?
Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 11, Gordon McMillan
"""
Packages: text.regular_expressions
"""
"""
> Why pattern ^[ \t] and ^[^ \t]* get the same result?. I understand
> the first one and it does as I expected. However, for the second
> one, I expected that ^[^ \t]* will not match " abc", since [^ \t] is
> a negation of [ \t] set. Can someone give me a bit of explanation?
It's a demonstration of how tricky [^] and * can be (note what the
first 5 letters of demonstration spell).
Translated to english, "^[^ \t]*" says:
starting at beginning of line
match any characters that are not space and not tab
any number of times.
Well guess what? " abc" starts with exactly 0 (any number) of
characters that are not (space or tab).
To be useful, get rid of the *.
To answer tomorrow's question today: note well the difference between
"match" and "search". You may think that
re.match('.*abc', ...)
is equivalent to
re.search('abc', ...)
but one of them's a lot faster than the other <wink>.
"""