简体   繁体   English

需要使一个正则表达式匹配具有不同格式的项目

[英]Need to make a regular expression match items with different formats

I'm trying to build a regular expression to match several items that follow an '=' sign. 我正在尝试构建一个正则表达式以匹配“ =”符号后的多个项目。 Here's what I have so far. 到目前为止,这就是我所拥有的。

= (\S+)(?=SNMP)

The result of that is matches on only 12 items of 14 from the following. 其结果是仅匹配以下14个项中的12个。

Enterprise OID: SNMPv2-SMI::enterprises.1453.1.1 Trap Type: Enterprise Specific  Trap Sub-Type: .5  Community/Infosec Context: TRAP, SNMP v1, community public  Uptime: 64593430  Description: Enterprise Specific  PDU Attribute/Value Pair Array:SNMPv2-SMI::enterprises.1453.2.1.1.1.1.0 = "Company"|SNMPv2-SMI::enterprises.1453.2.1.1.1.2.0 = "200Sou"|SNMPv2-SMI::enterprises.1453.2.1.1.1.3.0 = 22|SNMPv2-SMI::enterprises.1453.2.1.1.1.4.0 = "ABC TEST ALARM"|SNMPv2-SMI::enterprises.1453.2.1.1.1.5.0 = 1|SNMPv2-SMI::enterprises.1453.2.1.1.1.6.0 = 2|SNMPv2-SMI::enterprises.1453.2.1.1.1.7.0 = 0|SNMPv2-SMI::enterprises.1453.2.1.1.1.8.0 = 0|SNMPv2-SMI::enterprises.1453.2.1.1.1.9.0 = 0|SNMPv2-SMI::enterprises.1453.2.1.1.1.10.0 = 0|SNMPv2-SMI::enterprises.1453.2.1.1.1.11.0 = 0|SNMPv2-SMI::enterprises.1453.2.1.1.1.12.0 = ""|SNMPv2-SMI::enterprises.1453.2.1.1.1.13.0 = "2014-05-27"|SNMPv2-SMI::enterprises.1453.2.1.1.1.14.0 = "23:12:25"

According to Regexr , it's matching everything except the section with a trailing space, and the one not followed by 'SNMP'. 根据Regexr的说法 ,它会将节以外的所有内容与尾随空格匹配,并且将其后跟“ SNMP”。 Where am I going wrong here? 我在哪里错了?

The problem is \\S matches any non-white space character and you have whitespace in one of your values so it will fail to match here; 问题是\\S匹配任何非空白字符,并且您在一个值中包含空格,因此此处将匹配; to grab your last match add the end of line anchor $ inside your lookahead . 为了赢得你的最后一场比赛,在你的前瞻中添加行末锚$

= (.*?)(?=SNMP|$)

Regular expression: 正则表达式:

=               '= '
(               group and capture to \1:
 .*?            any character except \n (0 or more times)
)               end of \1
(?=             look ahead to see if there is:
 SNMP           'SNMP'
 |              OR
 $              before an optional \n, and the end of the string
)               end of look-ahead

Live Demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM