从一条长行中提取特定的字符串

rod*_*arg 2 python text-parsing

我正在尝试从包含多个ID的单个长行中提取某些网络接口的ID。我已经尝试使用split失败了。我将不胜感激

这是输入的示例,请记住这是在一行文本上。

"Authentication success on Interface Gi1/0/20 AuditSessionID 0000000XXXXXXXXXX, Authentication success on Interface Gi1/0/24 AuditSessionID 0000000XXXXXXXXXX, Authentication not succeed on Interface Fi1/0/10 AuditSessionID 0000000XXXXXXXXXX"

我期望输出仅为Gi1 / 0/20 Gi1 / 0/24 Fi1 / 0/10

Wil*_*lva 5

正则表达式适合此任务:

import re

text = 'Authentication success on Interface Gi1/0/20 AuditSessionID 0000000XXXXXXXXXX, Authentication success on Interface Gi1/0/24 AuditSessionID 0000000XXXXXXXXXX, Authentication not succeed on Interface Fi1/0/10 AuditSessionID 0000000XXXXXXXXXX'
re.findall('Interface (.*?) ', text)
Run Code Online (Sandbox Code Playgroud)

re.findall()会返回一个包含你想要的清单。

['Gi1/0/20', 'Gi1/0/24', 'Fi1/0/10']
Run Code Online (Sandbox Code Playgroud)

该模式'Interface (.*?) '通过匹配一切以单词“ Interface”开头,后跟一个空格,然后是某物或什么都不是,然后是另一个空格的方式工作。前面提到的某物或某物什么都没有用表示(.*?),它捕获(即添加到的输出中re.findall())与匹配的任何东西.*?,即任何字符(.),任意次数(*),匹配(?)所需的次数很少。您可以在https://regex101.com/等网站上使用正则表达式,这将使您能够运行Python正则表达式并对其进行解释(比我更好)。