在我的程序中,我使用正则表达式直到单词中断,然后再次使用它,直到单词停止。该方案的第一部分将比赛从军事时间转换为常规时间。第二部分按照用户输入的数字来划分军事时间。我的代码有效,但我使用了我的正则表达式两次。如何改变我的程序,所以我只使用一次正则表达式。使用正则表达式开始和停止
with open(filename) as text:
for line in text:
pattern = re.search(r'((((2)([0-3]))|(([0-1])([0-9])))([0-5])([0-9]))', line)
if pattern:
if re.match("BREAK", line):
break
for line in text:
m= re.search(r'((((2)([0-3]))|(([0-1])([0-9])))([0-5])([0-9]))', line)
if m:
if re.match("STOP", line):
break
're.match( “BREAK”,行)' - >'在line' – 2011-05-11 23:40:47
@Jochen Ritzel 'BREAK':没有,line.startswith(” BREAK“)给出了相同的含义。 re.match!= re.search。 – 2011-05-12 02:10:37