| | |
Search and replace with re:compile(....)
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
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?
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?
You are better off doing it this way and then process the resulting list ...
For a straight replace ...
python Syntax (Toggle Plain Text)
import re fl = re.compile('(abc|def|ghi)') ts = 'xyz abc mno def' q = fl.findall(ts) print q # ['abc', 'def']
python Syntax (Toggle Plain Text)
import re fl = re.compile('(abc|def|ghi)') ts = 'xyz abc mno def' ms = fl.sub('zzz', ts) print ms # xyz zzz mno zzz
Last edited by vegaseat; Jun 28th, 2008 at 10:05 am.
May 'the Google' be with you!
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:
python Syntax (Toggle Plain Text)
ts = 'xyz abc mno def' # separate at space to create a list space = ' ' qs = ts.split(space) # search for these items search_list = ['abc', 'def', 'ghi'] # append this string to search item s = 'zzz' # now create a new list with processed search items new_list = [] for q in qs: if q in search_list: # concatinate strings q += s new_list.append(q) # join the list to form the modified string new_ts = space.join(new_list) print new_ts # xyz abczzz mno defzzz
No one died when Clinton lied.
![]() |
Similar Threads
- my project is IDE for java (Java)
- Searching and Comparing strings from an XML Document (Python)
- Quick search & replace with c-strings? (C)
- https in C (C++)
- Help Me******* (Java)
- ftplib and the retrbinary() command (Python)
Other Threads in the Python Forum
- Previous Thread: Socket Server for game - too many threads?
- Next Thread: Widget Event Handling
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






