簡體   English   中英

重新查找所有功能

[英]re findall function

我有字符串:

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什么?

你可以拿下任何東西"

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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM