正则表达式捕获部分行 [英] Regex Capture Parts of Line
本文介绍了正则表达式捕获部分行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在努力捕捉 snmp 响应的一部分.
I have been struggling to capture a part of an snmp response.
文字
IF-MIB::ifDescr.1 = 1/1/g1, Office to DMZ
正则表达式
(?P<ifDescr>(?<=ifDescr.\d = ).*)
当前捕获
1/1/g1, Office to DMZ
如何仅捕获?
1/1/g1
Office 到 DMZ
编辑
1/1/g1
这应该与 snmp 响应中端口符号的数字和正斜杠匹配.
This should match the digit and forward slashes for the port notation in the snmp response.
(?P<ifDescr>(?<=ifDescr.\d = )\d\/\d\/g\d)
链接到 regexr
Office 到 DMZ
这应该通过端口符号开始匹配并捕获剩余的描述.
This should start the match past the port notation and capture remaining description.
(?P<ifDescr>(?<=ifDescr.\d = \d\/\d\/g\d, ).*)
链接到 regexr
推荐答案
我想这就是你要找的
= (.+), (.+)
它寻找=";然后捕获所有直到逗号,然后捕获所有内容.它返回
It looks for "= " then captures all until a comma and then everything afterwards. It returns
1/1/g1
Office to DMZ
根据要求.
在 regex101.com 上查看它.
这篇关于正则表达式捕获部分行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文