Both my regular expressions work, what I am trying to do is have the first one read the file and print every time there is a match(this works) then when it finds a match for the second expression it prints a match for it. So in the file I am importing, it will match all the numbers then once it finds ‘BREAK’ it print ‘BREAK’. What I am getting is that it prints ‘BREAK’ after every number it finds a match for. My goal is to stop the search for matching number once the program sees the word ‘BREAK’.
for m in re.finditer(r'((((2)([0-3]))|(([0-1])([0-9])))([0-5])([0-9]))', text):
print(m.group(0))
l=re.search(r'(BREAK)', text)
if l:
print(l.group(0))
Any ideas?
The problem is that when you search for “BREAK” you are searching the whole text and not the text that appears after the number you found. Because of this, if the text has “BREAK” anywhere in it, it will always be found.
It seems like
finditer()might not be the best thing to use for your situation. It would be better to iterate over the lines of the file yourself and you will have more control over the looping.