Regular Expression problem

Opinderjit bhellao at my-deja.com
Thu Sep 14 22:56:44 EDT 2000


I think your expression should work. Although, perhaps the (\W+) should
be a (\w+). \W matches non alpha numeric characters. I also added the
\s+.

import re
ttt = "Table Name Blue"
myregexx = re.compile(r'Table Name\s+(\w+)', re.IGNORECASE)
ssss = re.sub( myregexx , r'."""\nclass \1 (base_class):\n"""', ttt)
print ssss

Output:
."""
class Blue (base_class):
"""


You could also write the following:
ssss = myregexx.sub(r'."""\nclass \1 (base_class):\n"""', ttt)


In article <200009141200.HAA09752 at eye.neb>,
  vmilitaru at sympatico.ca wrote:
> Greetings. I am posting this in the hope that it will somehow end up
on comp.lang.python.
> I am using Python2b and can't get over the following RE problem:
>
> myregexx = re.compile(r'Table Name(\W+)', re.IGNORECASE)
> ssss = re.sub( myregexx , r'."""\nclass \1 (base_class):\n"""', ttt)
>
> I basically want to re-use the word matched with (\W+) as part of the
output. In Perl, I'd use the $1 variable.
> In Perl, I'd use the $1 variable. My initial understanding was that \1
is the Py equivalent.
> But since this obviously doesn't seem to be the case, and re-reading
the on-line docs for the 100th time doesn't seem to help anymore, could
somebody put me on the right track here.
>
> Regards,
> vio
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list