<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Found this in pypy!<br>
<br>
# Match any flavor of string; <i><b>the terminating quote is optional</b></i><br>
# so that we're robust in the face of incomplete program text.<br>
<br>
<tt>_match_stringre = re.compile(r"""<br>
    \""" [^"\\]* (?:<br>
                     (?: \\. | "(?!"") )<br>
                     [^"\\]*<br>
                 )*<br>
    (?: \""" )?<br>
<br>
|   " [^"\\\n]* (?: \\. [^"\\\n]* )* "?<br>
<br>
|   ''' [^'\\]* (?:<br>
                   (?: \\. | '(?!'') )<br>
                   [^'\\]*<br>
                )*<br>
    (?: ''' )?<br>
<br>
|   ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?<br>
""", re.VERBOSE | re.DOTALL).match<br>
</tt><br>
Problem solved.<br>
<br>
Ken<br>
<br>
<br>
Ken Seehart wrote:
<blockquote cite="mid:4B738AB1.2030702@seehart.com" type="cite"><br>
I found this:<br>
   <a moz-do-not-send="true" class="moz-txt-link-freetext"
 href="http://code.activestate.com/recipes/475109/">http://code.activestate.com/recipes/475109/</a><br>
  <br>
But it is incorrect in some cases, such as:<br>
  <b><br>
  <tt>"foo \" bar"</tt></b>  <i> (incorrectly matches "foo \")</i><br>
  <b><tt><br>
'''</tt></b>     <i>(incorrectly matches the second two single quotes)</i><br>
  <br>
  <tt><b>" foo<br>
  bar "</b></tt>  <i> (incorrectly matches quote containing newline</i>)<br>
  <br>
Anyone know a regular expression that correctly matches python string
literals?<br>
  <br>
Thanks in advance,<br>
Ken<br>
  <br>
</blockquote>
<br>
</body>
</html>