module re - \Q\E equivalent

Fredrik Lundh fredrik at pythonware.com
Sun May 11 08:57:30 EDT 2003


Martin Richtarsky wrote:

> In Perl, you can do m|\Qsomestring\E| to turn off metacharacter
> interpretation temporarily and match given string as is. Is this
> possible with re? I haven't found any metacharacters or flags that
> allow this.

you can use the re.escape function to escape the literal string:

    re.compile("somepattern" + re.escape(somestring))

</F>








More information about the Python-list mailing list