Regular expression to match a #

John Machin sjmachin at lexicon.net
Thu Aug 11 08:34:32 EDT 2005


Duncan Booth wrote:
> Dan wrote:
> 
> 
>>>My (probably to naive) approach is: p = re.compile(r'\b#include\b)
>>
>>I think your problem is the \b at the beginning. \b matches a word break
>>(defined as \w\W or \W\w). There would only be a word break before the #
>>if the preceding character were a \w (that is, [A-Za-z0-9_], and maybe
>>some other characters depending on your locale).
>>
>>However, the \b after the "include" is exactly what you want.
>>
> 
> 
> So the OP probably wanted '\B' the exact opposite of '\b' for the start of 
> the string, i.e. only match the # if it is NOT preceded by a wordbreak.
> 
> Alternatively for C style #includes search for r'^\s*#\s*include\b'.

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




More information about the Python-list mailing list