Regular expression to match a #
Duncan Booth
duncan.booth at invalid.invalid
Thu Aug 11 08:11:40 EDT 2005
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'.
More information about the Python-list
mailing list