Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to write replace string for object which will be substituted?[regexp]

Reply
Thread Tools

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

 
 
ryniek90
Guest
Posts: n/a
 
      08-04-2009
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 = '\(E-Mail Removed)\nname1 [at] mail [dot] com\nname2 [$at$]

mail [$dot$] com\n'
>>> mail

'\(E-Mail Removed)\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
com\n'
>>> print mail


http://www.velocityreviews.com/forums/(E-Mail Removed)
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = re.sub('^\n|$\n', '', mail)
>>> print maail

(E-Mail Removed)
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com
>>> maail = re.sub(' ', '', maail)
>>> print maail

(E-Mail Removed)
name1[at]mail[dot]com
name2[$at$]mail[$dot$]com
>>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
>>> print maail

(E-Mail Removed)
name1@mail[dot]com
name2@mail[$dot$]com
>>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
>>> print maail

(E-Mail Removed)
(E-Mail Removed)
(E-Mail 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.


 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      08-06-2009
In article <(E-Mail Removed)>,
ryniek90 <(E-Mail Removed)> wrote:
>
>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 ((E-Mail Removed)) <*> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to write replace string for object which will be substituted?[regexp] Ryniek90 Python 0 08-06-2009 09:03 PM
Re: How to write replace string for object which will be substituted?[regexp] MRAB Python 3 08-05-2009 11:40 AM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments