FAQ
Setting:
A function generates a string containing a regular expression. The
string must be used in two fashions:
1) It is printed into a file which contains python code to be later
executed.
2) It must be used immediately as a regexp.

For (1) backslashes must be escaped themselves several times, e.g.
space1 = "\\\\s" # space now contains two backslashes
print "space2 = '"+space1+"'"

The resulting text contains then
space2 = '\\s' # (*)
and consequently variable space2 will contain just one backslash.

Question:
Given that I have space1, is there a function which does the same
translation as does the python interpreter when interpreting/compiling
the line (*) such that in this case I end up with just one backslash?

Problem: Working with raw strings is currently not an option.


Thanks,
Harald Kirsch




--
----------------+------------------------------------------------------
Harald Kirsch | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172 | -- Paul Erd?s
*** Please do not send me copies of your posts. ***

Search Discussions

  • Marcin 'Qrczak' Kowalczyk at Apr 24, 2001 at 3:18 pm

    24 Apr 2001 16:55:28 +0200, Harald Kirsch <kirschh at lionbioscience.com> pisze:

    Given that I have space1, is there a function which does the same
    translation as does the python interpreter when interpreting/compiling
    the line (*) such that in this case I end up with just one backslash?
    Perhaps it would be easier to go the opposite way and apply repr to
    a string to add backslashes.

    --
    __("< Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
    \__/
    ^^ SYGNATURA ZAST?PCZA
    QRCZAK
  • Fredrik Lundh at Apr 24, 2001 at 4:30 pm

    Harald Kirsch wrote:
    1) It is printed into a file which contains python code to be later
    executed.
    file.write(repr(s))
    2) It must be used immediately as a regexp.
    assert re.match(re.escape(s), s) != None

    (or in other words, re.escape(s) returns a pattern that matches
    s if passed to the regular expression engine, for any possible s)

    Cheers /F

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 24, '01 at 2:55p
activeApr 24, '01 at 4:30p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase