ryniek90 wrote:
Hi.
I started learning regexp, and some things goes well, but most of them still not.

I've got problem with some regexp. Better post code here:

"
 >>> import re
>>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$] com\n'
 >>> mail
'\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$] com\n'
 >>> print mail

n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

 >>> maail = re.sub('^\n|$\n', '', mail)
 >>> print maail
n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com
 >>> maail = re.sub(' ', '', maail)
 >>> print maail
n...@mail.com
name1[at]mail[dot]com
name2[$at$]mail[$dot$]com
 >>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
 >>> print maail
n...@mail.com
na...@mail[dot]com
na...@mail[$dot$]com
 >>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
 >>> print maail
n...@mail.com
na...@mail.com
na...@mail.com
>>> #How must i write the replace string to replace all this regexp's with just ONE command, in string 'mail' ? >>> maail = re.sub('^\n|$\n| |\[at\]|\[\$at\$\]|\[dot\]|\[\$dot\$\]', *?*, mail)
"

How must i write that replace pattern (look at question mark), to maek that substituion work? I didn't saw anything helpful while reading Re doc and HowTo (from Python Doc). I tried with 'MatchObject.group()' but something gone wrong - didn't wrote it right.
Is there more user friendly HowTo for Python Re, than this?

I'm new to programming an regexp, sorry for inconvenience.

I don't think you can do it in one regex, nor would I want to. Just use
the string's replace() method.

>>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$] com\n'
>>> mail
'\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$] com\n'
>>> print mail

n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = mail.strip()
n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = maail.replace(' ', '')
>>> print maail
n...@mail.com
name1[at]mail[dot]com
name2[$at$]mail[$dot$]com
>>> maail = maail.replace('[at]', '@').replace('[$at$]', '@')
>>> print maail
n...@mail.com
na...@mail[dot]com
na...@mail[$dot$]com
>>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
>>> print maail
n...@mail.com
na...@mail.com
na...@mail.com
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to