Help needed: cryptic perl regular expression in python syntax

Antoon Pardon apardon at forel.vub.ac.be
Tue Oct 19 08:43:52 EDT 2004


Op 2004-10-19, pekka niiranen schreef <pekka.niiranen at wlanmail.com>:
> Hi there,
>
> I have perl script that uses dynamically
> constructed regular in this way:
>
> ------perl code starts ----
> $result "";
> $key = AAA\?01;
> $key = quotemeta $key;
> $line = "   s^\?AAA\?01^BBB^g; #Comment "
> if ($line =~ /(^\s*)(s|tr)(.)(\\?\??$key\??)\3(.*?)\3(.*)/) {
> 	$result = $5;
>
> # $result should be "BBB"
> # \3 gets the same value as returned by (.)
> # which is in this example ^. So we are searching
> # parameter limited by first two ^-signs
> # and returning the one limited byt the second
> # and third ^-sign. Note that using \3 in regular
> # expression enables other constants used than ^ -sign.
>
> ------perl code stops ----
>
> How can I construct equivalent python regural expression ?
>
> I have tested with constant regular expression like this:
>
> >>> line = '   s^\\?AAA\\?01^BBB^g; #Comment '
> >>> r1 = "(^\s*)(s|tr)(.)(\\\\\?\\\??AAA\\\\\?01)"
> >>> re.compile(r1).findall(line)
> [('   ', 's', '^', '\\?AAA\\?01')]
>
> Which is fine, but is there a way to join 3 raw strings
> together into another raw strings? like:
>
> r1 = r'''(^\s*)(s|tr)(.)(\\?\??'''
> r2 = r'''\\?\??)\3(.*?)\3(.*)'''
> p1 = r1 + key + r2 # p1 should remain raw string too
>

If I understand correctly there are no raw strings, just raw string
literals. The re.compile uses just a normal string.

raw string literal just make it easier to form a strings that are
typically used for regular expressions but the strings themselves
are just ordinary strings.

>>> s1="\\b"
>>> s2=r"\b"
>>> s1==s2
1
>>> s1
'\\b'
>>> s2
'\\b'
>>> print s1
\b
>>> print s2
\b
>>> 

-- 
Antoon Pardon



More information about the Python-list mailing list