我得到了这样的数据集(它以str形式从文件中打开):
MF8='out1mf8':'constant',[1944.37578865883]
MF9='out1mf9':'constant',[2147.79853787502]
MF10='out1mf10':'constant',[565.635908155949]
MF11='out1mf11':'constant',[0]
MF12='out1mf12':'constant',[0]
我需要将这些值放在括号中,因此创建的正则表达式:
^{pr2}$并使用:
re.findall(outmfPattern, f)
在mf = 9
之前,它工作得很好。有人知道怎么处理这个吗?在
让我们分解一下您的regex
out\dmf\d
:out
匹配序列'out'
\d
匹配一个数字mf
匹配序列'mf'
\d
匹配一个数字如果您想匹配
out1mf11
,那么需要在末尾查找2数字。在您可以使用
out\dmf\d+
,或者,如果您想在末尾只匹配1或2个数字,out\dmf\d{1,2}
。在此外,如果要在这些搜索项中添加括号,则可能应该查看
^{pr2}$re.sub
:re.sub
将捕获的组替换为包含在parens中的相同组。在相关问题 更多 >
编程相关推荐