Newbie backreference question

George Sakkis gsakkis at rutgers.edu
Thu Jun 30 18:37:45 EDT 2005


"paulm" <paulm at barley.vel.net> wrote:

> No, sorry - my bad. I am looking to assign the
> backreference to another variable so it can be treated
> seperately. So perhaps:
>
> $a = 'test string two';
> $a =~ /test \w{2}([\W\w]+)/;
> $b = $1;
> print $b . "\n";
>
> producing "ring two".
>
> I have read the docs and faqs but remain too dense
> to comprehend.
>
> Thanks again...

Did you have a look at my other reply ? It's still the same, just
change the regexp:

import re
a = 'test string two'
b = re.match(r'test \w{2}(.+)', a, re.DOTALL).group(1)
print b

By the way, if you want to catch any single character (including new
lines), '.' with re.DOTALL flag is more clear than [\W\w].

George




More information about the Python-list mailing list