我正在尝试从包含多个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
正则表达式适合此任务:
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)
该re.findall()
会返回一个包含你想要的清单。
['Gi1/0/20', 'Gi1/0/24', 'Fi1/0/10']
该模式'Interface (.*?) '
通过匹配以单词“ Interface”开头的所有内容,后跟一个空格,然后是某些东西或什么都不是,然后是另一个空格来进行工作。前面所说的什么都不是用表示(.*?)
,它捕获(即添加到的输出中re.findall()
)与匹配的任何东西.*?
,它是任意字符(.
),任意次数(*
),匹配(?
)所需的次数很少。您可以在https://regex101.com/之类的网站上使用正则表达式,这将允许您运行Python正则表达式,并对其进行解释(比我更好)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句