开发者

using two regular expressions to search a file

开发者 https://www.devze.com 2023-03-03 19:21 出处:网络
Both my regular expressions work, what I am trying to do is have the first one read the file and print every time the开发者_如何转开发re is a match(this works) then when it finds a match for the secon

Both my regular expressions work, what I am trying to do is have the first one read the file and print every time the开发者_如何转开发re 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.

with open("test.txt") as f:
  for line in f:
    if re.match("BREAK", line):
      print("Break")
      break
    m = re.match(r'((((2)([0-3]))|(([0-1])([0-9])))([0-5])([0-9]))', line)
    if m:
      print(m.group(0))


You evidently need to do :

import re

text = '''-- 000 == 111 ** 222
@@ 444 ## 555 BREAK 666 :::
777 ,,, 888 &&&'''

regx = re.compile('\d(\d)\d')
brek = text.find('BREAK')
li = [m.group() for m in regx.finditer(text,0,(brek if brek+1 else len(text)))]
print li
print
print '\n'.join(li)

result

['000', '111', '222', '444', '555']

000
111
222
444
555
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号