我从cisco switch命令输出了一个字符串:
interface FastEthernet2/0/45
switchport access vlan 703
switchport mode access
interface FastEthernet2/0/46
switchport access vlan 703
switchport mode access
interface FastEthernet2/0/47
switchport access vlan 703
switchport mode access
interface FastEthernet2/0/48
switchport access vlan 311
switchport mode access
interface GigabitEthernet2/0/1
interface GigabitEthernet2/0/2
interface GigabitEthernet2/0/3
interface GigabitEthernet2/0/4
我想使用pyhon re通过每个界面剪切它们,例如:
['interface FastEthernet2/0/45 switchport access vlan 703 switchport mode access',
..ommited.., 'interface FastEthernet2/0/48 switchport access vlan 311 switchport mode access',
..omitted.., 'interface GigabitEthernet2/0/3', 'interface GigabitEthernet2/0/4']
我记得它可以通过非贪婪来捕捉“界面”作为每个模式的停止点。不幸的是,这个正则表达式:“(interface。*?)”只获得每个“接口”,但是端口号和细节。我可以找到一个明显的结束字符,让我们得到“接口”并跟随字符串。
也许这很容易,但我完全打击了......
答案 0 :(得分:1)
Python不允许通过空匹配进行拆分,因此您必须编写正则表达式来匹配每个接口的所有行。
import re
re.findall(r'^interface.*(?:\n[\t ]+.*)*', input, re.M)
正则表达式从以^interface.*
开头的行开始匹配,然后匹配换行符,如果在下一行的开头有一个或多个空格[\t ]+
,则继续匹配。
记下重复开始时\n
的含义。如果输入不以新行结束,则此构造允许正则表达式匹配最后一条记录。
答案 1 :(得分:0)
根据interface
re.split(r'\s*\n\s*(?=interface\b)', s)
示例:的
>>> import re
>>> s = '''interface FastEthernet2/0/45
switchport access vlan 703
switchport mode access
interface FastEthernet2/0/46
switchport access vlan 703
switchport mode access
interface FastEthernet2/0/47
switchport access vlan 703
switchport mode access
interface FastEthernet2/0/48
switchport access vlan 311
switchport mode access
interface GigabitEthernet2/0/1
interface GigabitEthernet2/0/2
interface GigabitEthernet2/0/3
interface GigabitEthernet2/0/4'''
>>> re.split(r'\s*\n\s*(?=interface\b)', s)
['interface FastEthernet2/0/45\n switchport access vlan 703\n switchport mode access', 'interface FastEthernet2/0/46\n switchport access vlan 703\n switchport mode access', 'interface FastEthernet2/0/47\n switchport access vlan 703\n switchport mode access', 'interface FastEthernet2/0/48\n switchport access vlan 311\n switchport mode access', 'interface GigabitEthernet2/0/1', 'interface GigabitEthernet2/0/2', 'interface GigabitEthernet2/0/3', 'interface GigabitEthernet2/0/4']
答案 2 :(得分:0)
>>> re.findall(r"(interface\s*FastEthernet\d+\/\d+\/\d+[^\n]+\n[^\n]+\n[^\n]*)", YOUR_STRING, re.S|re.I)
['interface FastEthernet2/0/45\n switchport access vlan 703\n switchport mode access', 'interface FastEthernet2/0/46\n switchport access vlan 703\n switchport mode access', 'interface FastEthernet2/0/47\n switchport access vlan 703\n switchport mode access', 'interface FastEthernet2/0/48\n switchport access vlan 311\n switchport mode access']