Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > get back my simple little string after re search and replace

Reply
Thread Tools

get back my simple little string after re search and replace

 
 
Gontrand Trudau
Guest
Posts: n/a
 
      01-13-2010
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 :

['(E-Mail Removed)
;(E-Mail Removed) ;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 = """(E-Mail Removed)
;(E-Mail Removed) ;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 :

(E-Mail Removed)','(E-Mail Removed)','name, firstname','info','2010-01-01T00:00:00

Thank you

Gontrand
 
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
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
simple question reag search and replace sivga Perl Misc 2 11-07-2007 11:52 PM
A simple 'Replace' and 'Substring' question: How to add a string to another string SM Javascript 4 04-27-2007 07:22 AM
simple string search and replace Kun Python 4 03-26-2006 04:15 AM
How do I get back after a Search =?Utf-8?B?Um9uIFNtaXRo?= Wireless Networking 1 12-05-2005 12:51 PM



Advertisments