我正在解析文件,我想检查每行对一些复杂的正则表达式.这样的东西
if re.match(regex1, line): do stuff
elif re.match(regex2, line): do other stuff
elif re.match(regex3, line): do still more stuff
...
当然,要做的东西,我需要匹配的对象.我只能想到三种可能性,每种可能会留下一些希望.
if re.match(regex1, line):
m = re.match(regex1, line)
do stuff
elif re.match(regex2, line):
m = re.match(regex2, line)
do other stuff
...
这需要进行两次复杂的匹配(这些是长文件和长的正则表达式:/)
m = re.match(regex1, line)
if m: do stuff
else:
m = re.match(regex2, line)
if m: do other stuff
else:
...
随着我进一步深入,这变得可怕了.
while True:
m = re.match(regex1, line)
if m:
do stuff
break
m = re.match(regex2, line)
if m:
do other stuff
break
...
这看起来很奇怪.
这样做的正确方法是什么?