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

Discussion in 'Python' started by MRAB, Aug 4, 2009.

  1. MRAB

    MRAB Guest

    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, Aug 4, 2009
    #1
    1. Advertising

  2. MRAB

    ryniek Guest

    On 5 Sie, 00:55, MRAB <> 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 = '\\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
    >
    >
    >


    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? :p).
    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, Aug 5, 2009
    #2
    1. Advertising

  3. MRAB

    ryniek Guest

    On 5 Sie, 00:55, MRAB <> 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 = '\\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
    >
    >
    >


    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? :p).
    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, Aug 5, 2009
    #3
  4. MRAB

    Jon Clements Guest

    On 5 Aug, 07:53, ryniek <> wrote:
    > On 5 Sie, 00:55, MRAB <> 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 = '\\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
    > >
    > >
    > >

    >
    > 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?  :p).
    > 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   :)


    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, Aug 5, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ryniek90
    Replies:
    1
    Views:
    224
  2. Ryniek90
    Replies:
    0
    Views:
    211
    Ryniek90
    Aug 6, 2009
  3. Joao Silva
    Replies:
    16
    Views:
    344
    7stud --
    Aug 21, 2009
  4. Fabrizio J Bonsignore

    17 years that Win95 substituted Win3.1: publish full sources

    Fabrizio J Bonsignore, Jul 26, 2012, in forum: C Programming
    Replies:
    12
    Views:
    779
  5. Fabrizio J Bonsignore
    Replies:
    5
    Views:
    414
    Keith Thompson
    Jul 28, 2012
Loading...

Share This Page