help in converting perl re to python re

Mitja Trampus nun at example.com
Fri Mar 3 14:17:01 EST 2006


 >> i have some regular exp code in perl that i want to convert to python.
 >> if $line =~ m#<(tag1)>(.*)</\1>#
 >>    {
 >>      $variable = $2;
 >>     }

> regexp = re.compile(r"<(tag1)>(.*)</\1>")
> line = "<tag1>sometext</tag1>"
> match = regexp.search(line)
> if match:
>    variable = match.group(2)

Or, if you prefer shorter, quick-and-dirty syntax closer to perl's approach:

match = re.search(r"<(tag1)>(.*)</\1>", line)
if match: variable = match.group(2)



More information about the Python-list mailing list