How to write replace string for object which will be substituted?[regexp]

R

ryniek90

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 = '\[email protected]\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$] com\n'
>>> mail
'\[email protected]\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
com\n'
(e-mail address removed)
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com
(e-mail address removed)
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com
(e-mail address removed)
name1[at]mail[dot]com
name2[$at$]mail[$dot$]com
>>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
>>> print maail
(e-mail address removed)
name1@mail[dot]com
name2@mail[$dot$]com
>>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
>>> print maail
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)
>>> #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.
 
A

Aahz

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

1) 'Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.'
--Jamie Zawinski, comp.emacs.xemacs, 8/1997

2) If you really want to learn regexes, get a copy of _Mastering Regular
Expressions_ by Friedl (either 2nd or 3rd edition)
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings,
it's about treating strings as sequences of characters. The fact that
characters are also strings is the reason we have problems, but characters
are strings for other good reasons." --Aahz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top