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?