我有json字符串:
s = '''
{"messages":[],
"fileToTestMapForAllFiles":{"/pfe/usp/include/nat_util.h":[{"profile":"Component_toxicity_L2VPN_L2CKT_L3VPN_VPLS_VMX","occam":0,"releases":"15.1 and after","image":"junos-x86-64-*.vmdk","feature":"-","platform":"-","testSystem":"inception.juniper.net","enhancedIp":0,"id":0,"testId":"2","gating":"GATING","mandatory":""},
{"profile":"Component_toxicity_ipv46_bfd_ae_ifstate_VMX","occam":0,"releases":"15.1 and after","image":"junos-x86-64-*.vmdk","feature":"-","platform":"-","testSystem":"inception.juniper.net","enhancedIp":0,"id":0,"testId":"8","gating":"NON_GATING","mandatory":""},
{"profile":"Component_toxicity_GRES_NSR_VMX","occam":0,"releases":"15.1 and after","image":"junos-x86-64-*.vmdk","feature":"-","platform":"-","testSystem":"inception.juniper.net","enhancedIp":0,"id":0,"testId":"9","gating":"GATING","mandatory":""},
{"profile":"Component_toxicity_mcast_VMX","occam":0,"releases":"15.1 and after","image":"junos-x86-64-*.vmdk","feature":"-","platform":"","testSystem":"inception.juniper.net","enhancedIp":0,"id":0,"testId":"10","gating":"GATING","mandatory":""},
{"profile":"RPD_PC_Basic_Sanity_ST","occam":1,"releases":"15.1 and after","image":"junos-x86-64-*.vmdk,junos-x86-32-*.vmdk","feature":"rpd","platform":"vmx","testSystem":"inception.juniper.net","enhancedIp":0,"id":0,"testId":"129","gating":"GATING","mandatory":""},
{"profile":"RPD_PC_MISC","occam":1,"releases":"15.1 and after","image":"-","feature":"BBE-SANITY","platform":"mx-neo","testSystem":"inception.juniper.net","enhancedIp":1,"id":0,"testId":"387","gating":"GATING","mandatory":"18.2DCB,15.1F6-S10,17.2X75-D90,15.1R7,16.1R7,17.3R3,17.4R2,18.1R1,"}]},
"fileToTestMappingNotExist":[],"filesNotAdded":[],"validationFailedForTheseFiles":[],"filesToAdd":[],"invalidPathNames":[]}
'''
当我打电话时,我需要找到模式RPD_PC_<[anyword]>
:
print re.findall(r"RPD_PC_", s)
['RPD_PC_', 'RPD_PC_']
因为字符串包含字符串:
['RPD_PC_Basic_Sanity_ST'] ['RPD_PC_MISC']
哪种模式只会查找RPD_PC_MISC
,而不是RPD_PC_Basic_Sanity_ST
?
答案 0 :(得分:1)
你可以接受下一个"
的任何内容:
RPD_PC_[^"]+
[^"]+
会匹配下一个"
的一个或多个字符。
更强大的方法是确保匹配前面有"
:
"(RPD_PC_[^"]+)
re.findall
仅打印捕获的组匹配。
(零宽度)正面观察也会起作用:
(?<=")RPD_PC_[^"]+
如果您想明确匹配以下"
,请使用前瞻:
(?<=")RPD_PC_[^"]+(?=")
示例:强>
In [124]: re.findall(r'RPD_PC_[^"]+', s)
Out[124]: ['RPD_PC_Basic_Sanity_ST', 'RPD_PC_MISC']
In [125]: re.findall(r'"(RPD_PC_[^"]+)', s)
Out[125]: ['RPD_PC_Basic_Sanity_ST', 'RPD_PC_MISC']
In [126]: re.findall(r'(?<=")RPD_PC_[^"]+', s)
Out[126]: ['RPD_PC_Basic_Sanity_ST', 'RPD_PC_MISC']
修改强>
如果您只想匹配RPD_PC_MISC
,请匹配一个或多个不是_
/ "
的字符,后跟"
:
(?<=")RPD_PC_[^_"]+(?=")
所以:
In [127]: re.findall(r'(?<=")RPD_PC_[^_"]+(?=")', s)
Out[127]: ['RPD_PC_MISC']