【发布时间】:2020-09-15 13:13:40
【问题描述】:
我正在努力寻找匹配以下 2 种格式的正则表达式:
cmd1 = "cmd:ifconfig:"PASS":"Fail":4:2"
cmd2 = "cmd:ifconfig:"PASS""
以下是我的示例 python 代码
import re
cmd_reg = r'cmd:(.*):\"(.*?)\"$'
result=re.findall(cmd_reg,cmd2)
print(result) # output -> [('ifconfig', 'PASS')] Expectation [('ifconfig', 'PASS', 'FAIL', 4, 2)]
result=re.findall(cmd_reg,cmd1)
print(result) # output -> [] Expectation : [('ifconfig', 'PASS', '','','')]
但我无法弄清楚给出期望中提到的输出的正则表达式
【问题讨论】:
-
o/p是什么? -
类似
r'cmd:([^:]*):"([^"]*)"(?::"[^"]*":(\d+):")?$'?见regex101.com/r/woD4dU/1 -
我已经更新了我指的是 o/p 而不是我更新的输出
标签: python python-3.x regex python-re