在我的程序中,我使用正則表達式直到單詞中斷,然後再次使用它,直到單詞停止。該方案的第一部分將比賽從軍事時間轉換爲常規時間。第二部分按照用戶輸入的數字來劃分軍事時間。我的代碼有效,但我使用了我的正則表達式兩次。如何改變我的程序,所以我只使用一次正則表達式。使用正則表達式開始和停止
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