2011-05-11 80 views
0

在我的程序中,我使用正则表达式直到单词中断,然后再次使用它,直到单词停止。该方案的第一部分将比赛从军事时间转换为常规时间。第二部分按照用户输入的数字来划分军事时间。我的代码有效,但我使用了我的正则表达式两次。如何改变我的程序,所以我只使用一次正则表达式。使用正则表达式开始和停止

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 
+1

're.match( “BREAK”,行)' - >'在line' – 2011-05-11 23:40:47

+0

@Jochen Ritzel 'BREAK':没有,line.startswith(” BREAK“)给出了相同的含义。 re.match!= re.search。 – 2011-05-12 02:10:37

回答

5

首先,你的正则表达式r'((((2)([0-3]))|(([0-1])([0-9])))([0-5])([0-9]))'有一个荒谬的数目的括号。

假设您没有使用如此创建的捕获组。您似乎想要匹配HHMM,其中HH为00至23,MM为00至59.

r'(2[0-3]|[01][0-9])[0-5][0-9]将执行相同的工作。您可以通过执行r'(?:2[0-3]|[01][0-9])[0-5][0-9]'来避免剩下的一个捕获组。

您可能希望通过(例如)在模式的每个末端具有\b来避免虚假匹配(例如“blah 23456789”中的“2345”)。

这里是为您的代码替换:

import re 
searcher = re.compile(r'\b(?:2[0-3]|[01][0-9])[0-5][0-9]\b').search 
with open(filename) as text: 
     for line in text: 
      m = searcher(line) 
      if m: 
       do_something_1(line, m) 
      if line.startswith("BREAK"): # equivalent to your code; is that what you really mean?? 
       break 
     for line in text: 
      m = searcher(line) 
      if m: 
       do_something_2(line, m) 
      if line.startswith("STOP"): # equivalent to your code; is that what you really mean?? 
       break 
+0

谢谢。我知道我的正则表达式有许多括号。仍然习惯于小组 – Aaron 2011-05-12 02:14:28

2

最简单的方法是使用

my_re = re.compile("your regex") 
my_re.search(some_string) 
my_re.search(some_other_string) 
避免定义两次正则表达式

根据文档的内容,您可以拆分'BREAK'或匹配多个,难以理解,无需查看示例或更多定义。