我想知道下面的问题是否可以通过一个正则表达式来解决,或者我应该创建标准循环并逐行计算,
当我运行包含的代码时,我得到了['Ethernet0/22', 'Ethernet0/24'],只有结果应该是['Ethernet0/23', 'Ethernet0/25']。
对此有什么建议吗?
import re
txt='''#
interface Ethernet0/22
stp disable
broadcast-suppression 5
mac-address max-mac-count 1
port access vlan 452
#
interface Ethernet0/23
stp disable
description BTO
broadcast-suppression 5
port access vlan 2421
#
interface Ethernet0/24
stp disable
description Avaya G700
broadcast-suppression 5
port access vlan 452
#
interface Ethernet0/25
stp disable
description BTO
broadcast-suppression 5
port access vlan 2421
#
'''
re1 = '''^interface (.*?$).*?BTO.*?^#$'''
rg = re.compile(re1,re.IGNORECASE|re.DOTALL|re.MULTILINE)
m = rg.findall(txt)
if m:
print m发布于 2009-09-18 09:41:12
您的问题是正则表达式继续在下一组中查找BTO。作为一种快速的解决方法,您可以在接口id中禁用"#“字符(假设这在记录中无效,并且只将它们分开)。
re1 = '''^interface ([^#]*?$)[^#]*?BTO.*?^#$'''发布于 2009-09-18 11:58:40
这是一个针对你的文件的小的pyparsing解析器。这不仅显示了直接问题的解决方案,而且解析器为您提供了一组很好的对象,您可以使用这些对象轻松地访问每个接口中的数据。
下面是解析器:
from pyparsing import *
# set up the parser
comment = "#" + Optional(restOfLine)
keyname = Word(alphas,alphanums+'-')
value = Combine(empty + SkipTo(LineEnd() | comment))
INTERFACE = Keyword("interface")
interfaceDef = Group(INTERFACE + value("name") + \
Dict(OneOrMore(Group(~INTERFACE + keyname + value))))
# ignore comments (could be anywhere)
interfaceDef.ignore(comment)
# parse the source text
ifcdata = OneOrMore(interfaceDef).parseString(txt)现在如何使用它:
# use dump() to list all of the named fields created at parse time
for ifc in ifcdata:
print ifc.dump()
# first the answer to the OP's question
print [ifc.name for ifc in ifcdata if ifc.description == "BTO"]
# how to access fields that are not legal Python identifiers
print [(ifc.name,ifc['broadcast-suppression']) for ifc in ifcdata
if 'broadcast-suppression' in ifc]
# using names to index into a mapping with string interpolation
print ', '.join(["(%(name)s, '%(port)s')" % ifc for ifc in ifcdata ])打印输出:
['interface', 'Ethernet0/22', ['stp', 'disable'], ['broadcast-suppression', '5'], ['mac-address', 'max-mac-count 1'], ['port', 'access vlan 452']]
- broadcast-suppression: 5
- mac-address: max-mac-count 1
- name: Ethernet0/22
- port: access vlan 452
- stp: disable
['interface', 'Ethernet0/23', ['stp', 'disable'], ['description', 'BTO'], ['broadcast-suppression', '5'], ['port', 'access vlan 2421']]
- broadcast-suppression: 5
- description: BTO
- name: Ethernet0/23
- port: access vlan 2421
- stp: disable
['interface', 'Ethernet0/24', ['stp', 'disable'], ['description', 'Avaya G700'], ['broadcast-suppression', '5'], ['port', 'access vlan 452']]
- broadcast-suppression: 5
- description: Avaya G700
- name: Ethernet0/24
- port: access vlan 452
- stp: disable
['interface', 'Ethernet0/25', ['stp', 'disable'], ['description', 'BTO'], ['broadcast-suppression', '5'], ['port', 'access vlan 2421']]
- broadcast-suppression: 5
- description: BTO
- name: Ethernet0/25
- port: access vlan 2421
- stp: disable
['Ethernet0/23', 'Ethernet0/25']
[('Ethernet0/22', '5'), ('Ethernet0/23', '5'), ('Ethernet0/24', '5'), ('Ethernet0/25', '5')]
(Ethernet0/22, 'access vlan 452'), (Ethernet0/23, 'access vlan 2421'), (Ethernet0/24, 'access vlan 452'), (Ethernet0/25, 'access vlan 2421')发布于 2009-09-18 09:49:06
没有正则表达式的示例:
print [ stanza.split()[0]
for stanza in txt.split("interface ")
if stanza.lower().startswith( "ethernet" )
and stanza.lower().find("bto") > -1 ]解释:
我发现作文最好是“由内而外”:
for stanza in txt.split("interface ")在每次出现"interface“时拆分文本(包括以下空格)。生成的节将如下所示:
Ethernet0/22
stp disable
broadcast-suppression 5
mac-address max-mac-count 1
port access vlan 452
#接下来,过滤小节:
if stanza.lower().startswith( "ethernet" ) and stanza.lower().find("bto") > -1这应该是不言而喻的。
stanza.split()[0]在空格上拆分数学节,并将第一个元素放入结果列表中。这与筛选器startswith一起,将防止IndexError
https://stackoverflow.com/questions/1443433
复制相似问题