Search and replace with re:compile(....)

Thread Solved

Join Date: Jun 2008
Posts: 6
Reputation: s7plc is an unknown quantity at this point 
Solved Threads: 0
s7plc s7plc is offline Offline
Newbie Poster

Search and replace with re:compile(....)

 
0
  #1
Jun 28th, 2008
Ok, I would like to use a regular expression that I create dynamically to search and replace words in a file. For instance:


import re
fl = re.compile('abc|def|ghi')
ts = 'xyz abc mno def'

n = fl.search(ts)

print n

How would I find all matches in the string? So far, "n" only returns the first occurence.

Or, maybe I'm going about this wrong anyway. What I would like to do is find each match that is in my re:compile, and replace it with additional text appended to that match. So, my text string would end up being 'xyz match=abc mno match=def', as 'abc' and 'def' are items in my re. In my real world example, my re:compile string is created dynamically and will have anywhere from 10 to 50 items (this part of the code works fine), but I'm having a hard time making a one to one relationship with the item in the re:compile with what is actually found. Since regular expressions are supposed to be fast, I didn't explore using a loop yet, but maybe that's the only way. Advice?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Search and replace with re:compile(....)

 
0
  #2
Jun 28th, 2008
You are better off doing it this way and then process the resulting list ...
  1. import re
  2.  
  3. fl = re.compile('(abc|def|ghi)')
  4. ts = 'xyz abc mno def'
  5.  
  6. q = fl.findall(ts)
  7.  
  8. print q # ['abc', 'def']
For a straight replace ...
  1. import re
  2.  
  3. fl = re.compile('(abc|def|ghi)')
  4. ts = 'xyz abc mno def'
  5.  
  6. ms = fl.sub('zzz', ts)
  7.  
  8. print ms # xyz zzz mno zzz
Last edited by vegaseat; Jun 28th, 2008 at 10:05 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: s7plc is an unknown quantity at this point 
Solved Threads: 0
s7plc s7plc is offline Offline
Newbie Poster

Re: Search and replace with re:compile(....)

 
0
  #3
Jun 28th, 2008
Thanks very much. The fl.sub works like a charm.

But, how can I append the item that I am searching for with the replacement text? For instance, instead of "xyz zzz mno zzz" as a result, I would like "xyz abczzz mno defzzz".
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Search and replace with re:compile(....)

 
0
  #4
Jun 29th, 2008
In this case you are better off to use just Python. Actually, regular expressions aren't necessarily faster. I added plenty of comments to help you:
  1. ts = 'xyz abc mno def'
  2. # separate at space to create a list
  3. space = ' '
  4. qs = ts.split(space)
  5.  
  6. # search for these items
  7. search_list = ['abc', 'def', 'ghi']
  8.  
  9. # append this string to search item
  10. s = 'zzz'
  11.  
  12. # now create a new list with processed search items
  13. new_list = []
  14. for q in qs:
  15. if q in search_list:
  16. # concatinate strings
  17. q += s
  18. new_list.append(q)
  19.  
  20. # join the list to form the modified string
  21. new_ts = space.join(new_list)
  22.  
  23. print new_ts # xyz abczzz mno defzzz
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC