get back my simple little string after re search and replace

Discussion in 'Python' started by Gontrand Trudau, Jan 13, 2010.

  1. Hi,

    I have read the python doc pretty much, played around with python code,
    but unable to get back my "string" after made my replacement with python RE

    Here my string :

    ['
    ; ;name,
    firstname ;info;2010-01-01T00:00:00']

    I used Kodos (version 2.4.9) under ubuntu 9.04 to get this code :

    import re

    # common variables

    rawstr = r"""\s*\;"""
    embedded_rawstr = r"""\s*\;"""
    matchstr = """
    ; ;name,
    firstname ;info;2010-01-01T00:00:00"""

    # method 1: using a compile object
    compile_obj = re.compile(rawstr)
    match_obj = compile_obj.search(matchstr)

    # method 2: using search function (w/ external flags)
    match_obj = re.search(rawstr, matchstr)

    # method 3: using search function (w/ embedded flags)
    match_obj = re.search(embedded_rawstr, matchstr)

    # Replace string
    newstr = compile_obj.subn('','', 0)

    By the way I use Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    under ubuntu.

    My objective is to remove all the white space except the one between the
    name and the firstname, that is OK.

    I understand that re.compile compile my re stored in rawstr... match_obj
    return : <_sre.SRE_Match object at 0xb7777c28> that mean there were a
    match... I can extract information from this objet in a couple of way
    with : match_obj.group or match_obj.start...

    Sorry to be a newbie who is trying to make something easy but just felt
    on technical programmer stuff... :)

    I would just like to be able to do something like that :

    print match_obj

    and have the result of the modification I just did :

    ','','name, firstname','info','2010-01-01T00:00:00

    Thank you

    Gontrand
    Gontrand Trudau, Jan 13, 2010
    #1
    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. Kun
    Replies:
    4
    Views:
    312
    Dennis Lee Bieber
    Mar 26, 2006
  2. Author
    Replies:
    6
    Views:
    3,091
    George
    Jan 22, 2009
  3. sivga
    Replies:
    2
    Views:
    104
    John W. Krahn
    Nov 7, 2007
  4. Bruno

    Back (history.back()) after refresh

    Bruno, May 29, 2006, in forum: Javascript
    Replies:
    2
    Views:
    318
    Bruno Lopes
    May 30, 2006
  5. SM
    Replies:
    4
    Views:
    208
Loading...

Share This Page