quotes at the edges of a triple quote string...

Scott David Daniels Scott.Daniels at Acm.Org
Sun May 2 08:59:53 EDT 2004


Anthony Roberts wrote (looking to fix):
 >>> prop = re.compile("""(?P<name>[a-z0-9_\-]+)="(?P<value>.*?)""""re.I)

 > Peter Hansen wrote:
>> Can't you still use a triple-quoted string, but escape the
>> last quote?
>>  >>> """test\""""

> Yes... yes I can. I can also use the other quotetation marks. I realized 
> this just after I posted.
> 
>  >>> '''test"'''
> 'test"'

A couple of things to realize (esp if you are getting into longer regexps).

1) r"""abc""" is a raw string with a triple quote.
2) Also, 'abc' 'def' (string splicing) is _exactly_ the same as 'abcdef'
    The separate strings are glued together when converting the code to
    bytecode, so there is no cost of concatenation to worry about.

    What you might use for a regexp, then might be:

        prop = re.compile(r"(?P<name>[a-z0-9_\-]+)="
                          r'"(?P<value>.*?)"')

    The argument parens are enough for grouping, and the expression
     broken at appropriate points is arguably more readable.

-Scott



More information about the Python-list mailing list