Regular expression to match a #

John Machin sjmachin at lexicon.net
Thu Aug 11 16:48:42 EDT 2005


Aahz wrote:
> In article <42fb45d7$1 at news.eftel.com>,
> John Machin  <sjmachin at lexicon.net> wrote:
> 
>>Search for r'^something' can never be better/faster than match for 
>>r'something', and with a dopey implementation of search [which Python's 
>>re is NOT] it could be much worse. So please don't tell newbies to 
>>search for r'^something'.
> 
> 
> You're somehow getting mixed up in thinking that "^" is some kind of
> "not" operator -- it's the start of line anchor in this context.

I can't imagine where you got that idea from.

If I change "[which Python's re is NOT]" to "[Python's re's search() is 
not dopey]", does that help you?

The point was made in a context where the OP appeared to be reading a 
line at a time and parsing it, and re.compile(r'something').match() 
would do the job; re.compile(r'^something').search() will do the job too 
-- BECAUSE ^ means start of line anchor -- but somewhat redundantly, and 
very inefficiently in the failing case with dopey implementations of 
search() (which apply match() at offsets 0, 1, 2, .....).




More information about the Python-list mailing list