首页 > 解决方案 > Python Insightconnet 工作流程中的模式匹配

问题描述

我有一个检索 CVE 名称和编号的工作流。我可以让它按原样打印到 Teams 中。但是我试图只抓取 CVE 编号“CVE-2021-XXXXX”

当它运行为:

import re
text ="{{["Get Vulnerability Content from Rapid7 Vuln DB"].[content_result].[title]}}"

m = re.search(r'CVE-\d{4}-\d{4,7}', text)

if m:
    found = m.group(1)

我收到以下输出:

rapid7/Python 3 Script:2.0.3. Step name: run
Input: (below)

{}

Function: (below)

import re

text ="Google Chrome Vulnerability: CVE-2021-XXXX "Long description"

m = re.search(r'CVE-\d{4}-\d{4,7}', text)

if m:
    found = m.group(1)

Could not run supplied script. Error: no such group

我也尝试了 print() 和 Out。

它处于一个循环中,因此一次只会抓取一行文本。

标签: pythonregexworkflowrapid7

解决方案


在这里使用re.findall

text = 'Google Chrome Vulnerability: CVE-2021-XXXX "Long description"'
matches = re.findall(r'\bCVE-\d{4}-\w{4,7}\b', text)
print(matches)  # ['CVE-2021-XXXX']

推荐阅读