raw strings

Michele Simionato mis6 at pitt.edu
Fri Oct 11 09:28:52 EDT 2002


>... So I'm
> curious what use you have in mind.
> 
> Regards,
> Bengt Richter


For nearly all purposes, Python is the language which fits my mind the best.
However, there is an exception: regular expressions.

Python regular expressions are UGLY. 

Suppose for instance I want to substitute regexp1 with regexp2 in a text: 
in sed or perl I would give a command like

s/regexp1/regexp2/

In Python I must write 

import re
re.compile(r'regexp1').sub(r'regexp2',text)

which to my mind is ugly. Therefore I wanted to define a nicer substitution
command like this:

def sub(regexp1,regexp2,text):
    import re
    r1=raw_string(regexp1)
    r2=raw_string(regexp2)
    return re.compile(r1).sub(r2,text)

For this to work I need a raw_string function such that

raw_string('regexp')==r'regexp' 

Of course I could define the simpler

def sub(r1,r2,text):
    import re
    return re.compile(r1).sub(r2,text)

to be invoked as 

sub(r'regexp1',r'regexp2',text)

but to me the r in front of the string is ugly. This of course is only
an aesthetic issue, but I don't like to be forced to put the r in front
of raw strings, in the same way as I don't like languages where you
are forced to put additional symbols in the names of variables to specify 
their type. This is the reason why I would be happy to have a built-in 
raw_string function available.
Is there somebody else who thinks like me ?


-- 
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/



More information about the Python-list mailing list