Python 使用正则表达式启动和停止
在我的程序中,我使用正则表达式直到单词break,然后再次使用它直到单词stop。程序的第一部分将比赛从军事时间转换为常规时间。第二部分根据用户输入的数字划分军事时间。我的代码可以工作,但我使用正则表达式两次。怎样才能更改我的程序,使我只使用正则表达式一次Python 使用正则表达式启动和停止,python,regex,Python,Regex,在我的程序中,我使用正则表达式直到单词break,然后再次使用它直到单词stop。程序的第一部分将比赛从军事时间转换为常规时间。第二部分根据用户输入的数字划分军事时间。我的代码可以工作,但我使用正则表达式两次。怎样才能更改我的程序,使我只使用正则表达式一次 with open(filename) as text: for line in text: pattern = re.search(r'((((2)([0-3]))|(([0-1])([0-9]))
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
最简单的就是使用
my_re = re.compile("your regex")
my_re.search(some_string)
my_re.search(some_other_string)
这样可以避免两次定义正则表达式
根据文档的内容,您可以在“BREAK”上拆分或匹配多个,如果看不到示例或更多定义,就很难知道。首先,您的正则表达式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
关于matchBREAK,第->行“打断”line@Jochen里泽尔:不,行。startswithBREAK给出了相同的含义。重新匹配!=谢谢你。我知道我的正则表达式中有很多括号。仍然习惯于团队