Regular expression question
Duncan Booth
duncan at NOSPAMrcp.co.uk
Wed May 8 10:14:45 EDT 2002
Patrick Gaherty <pgaherty at chambersharrap.co.uk> wrote in
news:3CD8E9A7.6010600 at chambersharrap.co.uk:
> 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)
>
Use a function that returns the replacement text instead of a string:
def replace(match):
text = ('<a href="javascript:openWin(\'images/%s.htm\');">%s</a>'
% (match.group(1).lower(), match.group(2)))
return text
result = re.sub(r'<TABLE NAME="(.*?)">(.*?)</TABLE>', replace,
result)
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list