Newbie needs regex help

Dan M dan at catfolks.net
Mon Dec 6 10:48:50 EST 2010


On Mon, 06 Dec 2010 16:34:56 +0100, Alain Ketterlin wrote:

> Dan M <dan at catfolks.net> writes:
> 
>> I took at look at http://docs.python.org/howto/regex.html, especially
>> the section titled "The Backslash Plague". I started out trying :
> 
>>>>> import re
>>>>> r = re.compile('\\\\x([0-9a-fA-F]{2})') a = "This \xef file \xef has
>>>>> \x20 a bunch \xa0 of \xb0 crap \xc0
> 
> The backslash trickery applies to string literals also, not only
> regexps.
> 
> Your string does not have the value you think it has. Double each
> backslash (or make your string raw) and you'll get what you expect.
> 
> -- Alain.

D'oh! I hadn't thought of that. If I read my data file in from disk, use 
the raw string version of the regex, and do the search that way I do 
indeed get the results I'm looking for.

Thanks for pointing that out. I guess I need to think a little deeper 
into what I'm doing when I escape stuff.



More information about the Python-list mailing list