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

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

  1. ryniek90

    ryniek90 Guest

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

  2. ryniek90

    Aahz Guest

    In article <>,
    ryniek90 <> 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 () <*> 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
     
    Aahz, Aug 6, 2009
    #2
    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. MRAB
    Replies:
    3
    Views:
    270
    Jon Clements
    Aug 5, 2009
  2. Ryniek90
    Replies:
    0
    Views:
    223
    Ryniek90
    Aug 6, 2009
  3. Joao Silva
    Replies:
    16
    Views:
    363
    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:
    792
  5. Fabrizio J Bonsignore
    Replies:
    5
    Views:
    418
    Keith Thompson
    Jul 28, 2012
Loading...

Share This Page