Saturday 15 June 2013

Using regex in Python 2.7.3 to search text and output matches -


I'm trying to complete what the title says. The program means to read the .txt file from the specified path and match the words specified in the code. This is what I have done so far:

  import source = open ("c: \\ test.txt", "r") lines = [] for the line in the source: line = Line ('answer', line): print 'found:', line   

As you can see, I'm specifying the word again using the answer 'answer' But it restricts me to a word. I know that there is a way to specify a list or dictionary word for search, but my attempt has failed. I think it is possible to make a list with something like this ...

  keywords = ['answer', 'error', 'what']   < P> ... but despite what I've read on this site, I can not include it properly in the code. Any advice or assistance with this is appreciated!  

If I want to make search case sensitive to PS, can I use it ...

  ". (* * * (* R *)   

The only way : import re source = open ("input", "r") line = [] keyword = ['answer', 'error', 'do'] # or list with OR Join in, '|', operator # again. I add it to the case-irreversible exp = re.compile ("|". (Keyword), again. For areas: line = Lainkstrip () lines Kappend (line) if rediscovered (XP line): print found '', Line

No comments:

Post a Comment