Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Re: How to write replace string for object which will be substituted?[regexp]

 
Thread Tools Search this Thread
Old 08-04-2009, 11:55 PM   #1
Default Re: How to write replace string for object which will be substituted?[regexp]


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 = '\\nname1 [at] mail [dot] com\nname2 [$at$]

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

> '\\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> com\n'
> >>> print mail

>
>
> name1 [at] mail [dot] com
> name2 [$at$] mail [$dot$] com
>
> >>> maail = re.sub('^\n|$\n', '', mail)
> >>> print maail

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

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

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

>
>
>
> >>> #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 = '\\nname1 [at] mail [dot] com\nname2 [$at$]

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

'\\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
com\n'
>>> print mail



name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = mail.strip()


name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = maail.replace(' ', '')
>>> print maail


name1[at]mail[dot]com
name2[$at$]mail[$dot$]com
>>> maail = maail.replace('[at]', '@').replace('[$at$]', '@')
>>> print maail


name1@mail[dot]com
name2@mail[$dot$]com
>>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
>>> print maail






MRAB
  Reply With Quote
Old 08-05-2009, 07:50 AM   #2
ryniek
 
Posts: n/a
Default Re: How to write replace string for object which will be substituted?[regexp]
On 5 Sie, 00:55, MRAB <pyt...@mrabarnett.plus.com> wrote:
> 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
> > name1@mail[dot]com
> > name2@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
> name1@mail[dot]com
> name2@mail[$dot$]com
> *>>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
> *>>> print maail
> n...@mail.com
> na...@mail.com
> na...@mail.com


Too bad, I thought that the almighty re module could do anything, but
it failed with this (or maybe re can do what i want, but only few
people knows how to force him to that? ).
But with help of MRAB, i choose The 3rd Point of Python's Zen -
"Simple is better than complex."

"
>>> mail = '\\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$] com\n'
>>> mail

'\\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
com\n'
>>> print mail



name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = mail.lstrip().rstrip().replace(' ', '').replace('[dot]', '.')..replace('[$dot$]', '.').replace('[at]', '@').replace('[$at$]', '@')
>>> print maail




>>> #Did it

"

Thanks again


ryniek
  Reply With Quote
Old 08-05-2009, 07:53 AM   #3
ryniek
 
Posts: n/a
Default Re: How to write replace string for object which will be substituted?[regexp]
On 5 Sie, 00:55, MRAB <pyt...@mrabarnett.plus.com> wrote:
> 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
> > name1@mail[dot]com
> > name2@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
> name1@mail[dot]com
> name2@mail[$dot$]com
> *>>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
> *>>> print maail
> n...@mail.com
> na...@mail.com
> na...@mail.com


Too bad, I thought that the almighty re module could do anything, but
it failed with this (or maybe re can do what i want, but only few
people knows how to force him to that? ).
But with help of MRAB, i choose The 3rd Point of Python's Zen -
"Simple is better than complex."

"
>>> 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.lstrip().rstrip().replace(' ', '').replace('[dot]', '.')..replace('[$dot$]', '.').replace('[at]', '@').replace('[$at$]', '@')
>>> print maail

n...@mail.com
na...@mail.com
na...@mail.com
>>> #Did it


"

Thanks again


ryniek
  Reply With Quote
Old 08-05-2009, 12:40 PM   #4
Jon Clements
 
Posts: n/a
Default Re: How to write replace string for object which will be substituted?[regexp]
On 5 Aug, 07:53, ryniek <rynie...@gmail.com> wrote:
> On 5 Sie, 00:55, MRAB <pyt...@mrabarnett.plus.com> wrote:
>
>
>
> > 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
> > > name1@mail[dot]com
> > > name2@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
> > name1@mail[dot]com
> > name2@mail[$dot$]com
> > *>>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
> > *>>> print maail
> > n...@mail.com
> > na...@mail.com
> > na...@mail.com

>
> Too bad, I thought that the almighty re module could do anything, but
> it failed with this (or maybe re can do what i want, but only few
> people knows how to force him to that? *).
> But with help of MRAB, i choose The 3rd Point of Python's Zen -
> "Simple is better than complex."
>
> "
>
> >>> 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.lstrip().rstrip().replace(' ', '').replace('[dot]', '.').replace('[$dot$]', '.').replace('[at]', '@').replace('[$at$]', '@')
> >>> print maail

>
> n...@mail.com
> na...@mail.com
> na...@mail.com
>
> >>> #Did it *

>
> "
>
> Thanks again *


Short of writing a dedicated function I might be tempted to write this
as:

EMAIL_REPLACEMENTS = (
('[at]', '@'),
('[dot]', '.'),
...
)

for src, dest in EMAIL_REPLACEMENTS:
mail = mail.replace(src, dest)

Apart from taste reasons, it keeps the replaces more obvious (and
accessible via a variable rather than embedded in the code), enables
swapping the order or adding/removing easier.

Jon



Jon Clements
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
Java String Problems rbnbenjamin General Help Related Topics 0 02-03-2009 11:02 PM
ASP.NET: Asign Users in Roles(Array.IndexOf(Of String) method) msandlana Software 0 04-25-2008 06:37 AM
Hidden linebreaks in string? VB.NET Jiggy Software 0 04-23-2008 02:18 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46