Substitution with Hash Values

Jon Ribbens jon+usenet at unequivocal.co.uk
Sat Apr 20 11:27:10 EDT 2002


In article <mailman.1019315200.29890.python-list at python.org>, holger krekel wrote:
>> >> Need a raw string there, i.e.
>> >> 
>> >>   r"\{\{(.*?)\}\}"
>> > 
>> > yes, i am sometimes confused by 'raw strings' and 'strings'. 
>> > Could you help by saying why exactly it is needed here?
>> 
>> Hmm, well, actually you don't, since \{ is not a string literal escape
>> sequence and Python is weird and leaves in unknown escape sequences
>> unchanged.
> 
> Actually my intention was to prevent the re module from thinking
> i might mean 'repetition' with {...}. I thought that every special
> regexp-character like '.' or '*' needs to be escaped if you want
> it literally. I was surprised to see that you can omit the '\'
> in the above case

Um, I didn't omit the \. I expect you are right that you do need it to
escape it from the regexp parser. I was wrong that you need to use a
raw string to prevent the Python parser from thinking the \ means
something, because in the specific case of \{ the Python parser
doesn't do anything with it. It is still a good idea to use
raw strings always for regexps though, because then you're not relying
on things turning out right accidentally.

> But is it true that you 'never' use them somewhere else? 
> I would think that one would like to use 'r' whenever you don't 
> want the escape-sequence-string-interpretation which is not really
> restricted to regexp-strings. 

True, but personally I've never found any other use for them.



More information about the Python-list mailing list