regex to match python string literal syntax

Ken Seehart ken at seehart.com
Thu Feb 11 07:28:43 CET 2010


Found this in pypy!

# Match any flavor of string; /*the terminating quote is optional*/
# so that we're robust in the face of incomplete program text.

_match_stringre = re.compile(r"""
    \""" [^"\\]* (?:
                     (?: \\. | "(?!"") )
                     [^"\\]*
                 )*
    (?: \""" )?

|   " [^"\\\n]* (?: \\. [^"\\\n]* )* "?

|   ''' [^'\\]* (?:
                   (?: \\. | '(?!'') )
                   [^'\\]*
                )*
    (?: ''' )?

|   ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?
""", re.VERBOSE | re.DOTALL).match

Problem solved.

Ken


Ken Seehart wrote:
>
> I found this:
>    http://code.activestate.com/recipes/475109/
>
> But it is incorrect in some cases, such as:
> *
> "foo \" bar"*  / (incorrectly matches "foo \")/
> *
> '''*     /(incorrectly matches the second two single quotes)/
>
> *" foo
>   bar "*  / (incorrectly matches quote containing newline/)
>
> Anyone know a regular expression that correctly matches python string 
> literals?
>
> Thanks in advance,
> Ken
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100210/689924eb/attachment.html>


More information about the Python-list mailing list