Regular expression question

Noah noah at noah.org
Wed May 8 10:38:27 EDT 2002


I think you will be forced to do this in three steps:
pull out the matched groups into variables;
modify the variables; then do your substitution.

The replacement can also be a function. Maybe if you are smart
and want unreadable code you could do this with a lambda or something.
The following will substitute the matched groups as lowercase, but I'm
unsure how to substitute this into a new string.

import re
def myLower (match):
    "This returns the matched string in lower case."
    return match.group().lower()

pat = re.compile(r'<TABLE NAME="(.*?)">(.*?)</TABLE>')
pat.sub(myLower, r'<TABLE NAME="FOO">FOOBAR</TABLE>')

I don't know if this helps. Maybe it's a clue.

Yours,
Noah

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Patrick Gaherty
Sent: Wednesday, May 08, 2002 2:03 AM
To: python-list at python.org
Subject: Regular expression question


Im performing the following search and replace in Python. However, I 
would like to return the first group as lowercase using .lower() in the 
replacement string, What's the best way of achieving this?

result = re.sub(r'<TABLE NAME="(.*?)">(.*?)</TABLE>', 
r'<a href="javascript:openWin(\'images/\1.htm\');">\2</a>', result)

Thanks

Patrick Gaherty







More information about the Python-list mailing list