Substitution with Hash Values

holger krekel pyth at devel.trillke.net
Sat Apr 20 11:05:08 EDT 2002


On Sat, Apr 20, 2002 at 02:37:34PM -0000, Jon Ribbens wrote:
> In article <mailman.1019303438.1339.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
 
> Basically, you always use raw strings when using regexps, and never
> use them anywhere else. You need them because both string literals and
> regexps treat \ as an escape character, so to get the \ through to the
> regexp unchanged you use a raw string.

thanks for the explanation! 

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. 

I have noticed that e.g. the docstring of the module os.py uses
r'...'

regards,

	holger





More information about the Python-list mailing list