[Tutor] question regarding regular expression compile

Steven D'Aprano steve at pearwood.info
Thu Jan 13 12:31:08 CET 2011


Luke Paireepinart wrote:
> No. Did you try that? It doesn't evn look like valid python code to me.
> You want a single string with the r before it, not 3 separate strings.

The line of code in question is:

>>         test = re.compile('MAT file (billing|carrier|log|util)' r'\\' '\d{8} deleted')

If you actually try it, it is perfectly valid Python code :)

However, I agree with Luke: this would be better written as a single string:

'MAT file (billing|carrier|log|util)\\\\\d{8} deleted'

or even better, a raw string

r'MAT file (billing|carrier|log|util)\\\d{8} deleted'


Little know Python fact: Python includes implicit concatenation of 
string literals. If you have two or more string literals (but not 
variables) written next to each other, Python will automatically 
concatenate them at compile time. You can mix quotation marks and raw 
and ordinary strings as needed:

 >>> print "Hello" 'world'
Helloworld

Here's a more useful example:

if condition:
     raise ValueError("this is a very long"
     " error message with much useful detail,"
     " far too much to fit on a single line of"
     " source code.")

which is equivalent to this:

if condition:
     s = "this is a very long"
     s += " error message with much useful detail,"
     s += " far too much to fit on a single line of"
     s += " source code."
     raise ValueError(s)

except the string is constructed once, at compile time, and no variable 
s is created.



-- 
Steven


More information about the Tutor mailing list