找到接口

时间:2018-04-03 15:33:22

标签: python regex

我不擅长RegEx。我需要帮助来识别以Fast / Giga一词开头的第一个单词。 RegEx我尝试了"^\s*(?=Fast|Giga).\w+",但它没有从' /'中添加,需要全名,例如" GigabitEthernet2/47"它匹配" up" " down"分开。

GigabitEthernet2/47 (this should belong to up)
GigabitEthernet2/48 (this should belong to down)
FastEthernet3/1 (this should belong to down)
FastEthernet3/2 (this should belong to down)
FastEthernet3/3 (this should belong to down)

单独使用RegEx还需要RegEx

"Last input 00:00:09, output 00:00:00, output hang never"

输入,输出和输出挂起需要三个RegEx。无论如何 旁边(输入,输出和输出挂起)应该只显示。

00:00:09 (this should belong to input)
00:00:00 (this should belong to output)
never    (this should belong to output hang)

interface GigabitEthernet6/48
 description ***connected to Panel***
 switchport access vlan 10
 switchport mode access
 spanning-tree portfast
!
interface GigabitEthernet7/1
!
GigabitEthernet2/47 is up, line protocol is down (notconnect)
  Hardware is Gigabit Ethernet Port, address is 0023.0460.5a4e (bia 0023.0460.5a4e)
  Last input 00:00:09, output 00:00:00, output hang never
GigabitEthernet2/48 is down, line protocol is down (notconnect)
  Hardware is Gigabit Ethernet Port, address is 0023.0460.5a4f (bia 0023.0460.5a4f)
  Last input 42w6d, output 42w6d, output hang never
FastEthernet3/1 is down, line protocol is down (notconnect)
  Hardware is Fast Ethernet Port, address is 0022.906f.4040 (bia 0022.906f.4040)
   Last input 00:00:58, output 00:00:00, output hang never
FastEthernet3/2 is down, line protocol is down (notconnect)
  Hardware is Fast Ethernet Port, address is 0022.906f.4041 (bia 0022.906f.4041)
  Last input never, output never, output hang never
FastEthernet3/3 is administratively down, line protocol is down (disabled)
  Hardware is Fast Ethernet Port, address is 0022.906f.4042 (bia 0022.906f.4042)
  Last input never, output never, output hang never
TenGigabitEthernet4/6 is down, line protocol is down (inactive)
  Hardware is Ten Gigabit Ethernet Port, address is 843d.c632.a74d (bia 843d.c632.a74d)
  Last input never, output never, output hang never

2 个答案:

答案 0 :(得分:1)

对于你需要的第一个正则表达式,你有第一部分。现在,您只需要匹配dll256.dll及其后面的数字。

/

首先,我添加了^\s*(?=Fast|Giga).\w+/\d+ 以匹配斜杠字符。然后,我添加了/\d+匹配数字字符,也称为数字。 \d修改了+符号,使其至少匹配一个数字字符。

编辑:要匹配,如果它已关闭,请使用

\d

为此,请使用

(^\s*(?=Fast|Giga).\w+/\d+) is down

答案 1 :(得分:1)

以下内容将捕获您需要的名称:

(^(?:Giga|Fast)\S+)

现在分开上下匹配(最初将它们组合在一起的原因是为了确保updown仅在Giga或{{{ 1}}):

Fast

<强>解释

断言行开始和非捕获组以匹配Giga或Fast(因为整个匹配包含在外部捕获组中),然后是一个或多个非空白字符:

(?:.*?)(up|down)(?:.*?)(up|down)

非贪婪的非捕获组,以匹配导致上升或下降的所有内容(重复两次以满足您的需求):

(^(?:Giga|Fast)\S+)

以下内容将捕获输入,输出和输出挂起:

(?:.*?)(up|down)(?:.*?)(up|down)

<强>解释

各种选项的非捕获组,输入或输出(带有可选挂起),如果到达逗号(?:(?<=input )|(?<=output )(?:hang )?)(.*?)(?:\,|$) 或行尾,则终止匹配。