简体   繁体   English

正则表达式匹配Cisco接口

[英]Regex matching Cisco interface

I am trying to match Cisco's interface names and split it up. 我试图匹配思科的接口名称并将其拆分。 The regex i have so far is: 我到目前为止有的正则表达式是:

(\D+)(\d+)(?:\/)?(\d+)?(?:\.)?(\d+)?

This matches: 这符合:

FastEthernet9
FastEthernet9/5
FastEthernet9/5.10

The problem i have is that it also matches: 我的问题是它也匹配:

FastEthernet9.10

Any ideas on how to make it so it does not match? 关于如何使它不匹配的任何想法? Bonus points if it can match: 可以匹配的奖励积分:

tengigabitethernet0/0/0.20

Edit: 编辑:

Okay. 好的。 I am trying to split this string up into groups for use in python. 我试图将此字符串分成几组,以便在python中使用。 In the cisco world the first part of the string FastEthernet is the type of interface, the first zero is the slot in the equipment the zero efter the slash is the port number and the one after the dot is a sub-interface. 在cisco世界中,字符串FastEthernet的第一部分是接口的类型,第一个零是设备中的插槽,之后的零是斜杠是端口号,而点后的是子接口。

Because of how regex works i can't get dynamic groups like (?:\\/?\\d+)+ to match all numbers in /0/0/0 by them selves, but i only get the last match. 由于正则表达式的工作方式,我无法获得像(?:\\/?\\d+)+这样的动态组来自己匹配/0/0/0 0/0/0中的所有数字,但我只能得到最后一个匹配项。

My current regex (\\D+)(\\d+)(?:((?:\\/?\\d+)+)?(?:(?:\\.)?(\\d+))?) builds on murgatroid99's but groups all /0/0/0 together, for splitting in python. 我当前的正则表达式(\\D+)(\\d+)(?:((?:\\/?\\d+)+)?(?:(?:\\.)?(\\d+))?) /0/0/0一起,用于在python中拆分。

My current result in python with this regex is [('tengigabitethernet', '0', '/0/0', '10')] . 我目前在此正则表达式的python中的结果是[('tengigabitethernet', '0', '/0/0', '10')] This seems to be how close i can get. 这似乎是我能接近的程度。

The regular expression for matching these names (Removing unnecessary capturing groups for clarity) is: 匹配这些名称的正则表达式(为清楚起见,删除了不必要的捕获组)为:

\D+\d+((/\d+)+(\.\d+)?)?

To break it up, \\D+ matches the part of the string before the first number (such as FastEthernet and \\d+ matches the first number (such as 10 ). Then the rest of the pattern is optional. /\\d+ matches a forward slash followed by a number, so (/\\d+)+ matches any number of repetitions of that (such as /0/0 ). Finally, (\\.\\d+)? optionally matches the period followed by a number at the end. 为了解决这个问题, \\D+匹配字符串中第一个数字之前的部分(例如FastEthernet\\d+匹配第一个数字(例如10 )。然后模式的其余部分是可选的。/ /\\d+匹配正斜杠。后跟一个数字,因此(/\\d+)+匹配该数字的任意重复(例如/0/0 )。最后, (\\.\\d+)?可选地匹配句号,最后是一个数字。

The important difference that makes this pattern match your specification is that in the final optional group, we get at least one (/\\d+) before the (\\.\\d) . 使此模式符合您的规范的重要区别在于,在最后一个可选组中,我们在(\\.\\d)之前至少得到一个(/\\d+) (\\.\\d)

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

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