pattern = re.compile('''first_condition\((.*)\)
extra_condition\((.*)\)
testing\((.*)\)
other\((.*)\)''', re.UNICODE)
那会发生什么,如果我运行下面的文字正则表达式:
text = '''first_condition(enabled)
extra_condition(disabled)
testing(example)
other(something)'''
result = pattern.findall(text)
print(result)
[('enabled', 'disabled', 'example', 'something')]
但是如果一个或两行被遗漏,正则表达式返回空列表。例如。我的文字是:
text = '''first_condition(enabled)
other(other)'''
我要得到什么:
[('enabled', '', '', 'something')]
我可以在几个命令做到这一点,但我认为,这将是比一个正则表达式做慢。原始代码使用sed,所以速度非常快。我可以用sed来做,但我需要跨平台的方式来做到这一点。有可能吗? Tnanks!
P.S.这也将是巨大的,如果字符串的顺序将是免费的,不是固定不变的:
text = '''other(other)
first_condition(enabled)'''
必须返回绝对是一样的:
[('enabled', '', '', 'something')]
+2
你真的想要得到的结果是包含一个元组的列表? –
+0
@MarkByers:不,单个列表甚至更好。 –
+0
或单个元组,不需要。 –