faqts : Computers : Programming : Languages : Python : Snippets : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 3 people (100%) answered Yes
Recently 2 of 2 people (100%) answered Yes

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