首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将缺失的值与regex匹配

如何将缺失的值与regex匹配
EN

Stack Overflow用户
提问于 2015-10-29 08:40:33
回答 1查看 1.1K关注 0票数 1

我有一个表(在本例中是字符串),如下所示:

代码语言:javascript
复制
Community            Group / Access      context    acl_filter
---------            --------------      -------    ----------
Community_test-1      network-operator    C0n_text!  T3st-ACL#$
WEWORK                network-operator
RW                    network-admin       _C0n              
YANKS                 network-admin                  my_acl

我必须使用单个regex来匹配和解析所有值,而不需要使用额外的代码,但是编写正则表达式匹配缺少的值也有问题。

现在,让我们忽略前两行,关注实际值:

代码语言:javascript
复制
import re
import pprint

show_snmp_community = """
Community_test-1      network-operator    C0n_text!  T3st-ACL#$
WEWORK                network-operator
RW                    network-admin       _C0n              
YANKS                 network-admin                  my_acl"""

show_snmp_community_regex = "(?P<community>\S+)\s+(?P<group_access>\S+)\s+(?P<context>\S+)\s+(?P<acl>\S+)"
show_snmp_community_split = show_snmp_community.split('\n')
final_dict = {}
i = 0
for line in show_snmp_community_split:
    snmp_dict = {}
    match_snmp = re.match(show_snmp_community_regex, line)
    group_snmp = match_snmp.groupdict()
    community = group_snmp["community"]
    snmp_dict["community"] = community
    group_access = group_snmp["group_access"]
    snmp_dict["group_access"] = group_access
    context = group_snmp["context"]
    snmp_dict["context"] = context
    acl = group_snmp["context"]
    snmp_dict["acl"] = acl
    final_dict[i] = snmp_dict
    i += 1
pretty = pprint.PrettyPrinter(indent=2, depth=10).pprint
pretty(final_dict)

显然,这导致:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\Gabriele\Desktop\prova.py", line 18, in <module>
    group_snmp = match_snmp.groupdict()
AttributeError: 'NoneType' object has no attribute 'groupdict'

这就是我想要达到的目标:

代码语言:javascript
复制
{ 0: { 'acl': 'C0n_text!',
       'community': 'Community_test-1',
       'context': 'C0n_text!',
       'group_access': 'network-operator'}
1: { 'acl': '',
       'community': 'WEWORK',
       'context': '',
       'group_access': 'network-operator'}
2: { 'acl': '',
       'community': 'RW',
       'context': '_C0n',
       'group_access': 'network-admin'}
3: { 'acl': 'my_acl',
       'community': 'YANKS',
       'context': '',
       'group_access': 'network-admin'}}

我用不同的正则表达式尝试过许多东西,但没有成功:

EN

回答 1

Stack Overflow用户

发布于 2015-10-29 09:01:34

引发此错误是因为,当您将原始字符串拆分为show_snmp_community_split时,第一个元素是一个空字符串:''。与re.match一起使用时,这将返回None

在循环中使用if-语句:

代码语言:javascript
复制
for line in show_snmp_community_split:
    snmp_dict = {}
    match_snmp = re.match(show_snmp_community_regex, line)
    if not match_snmp:
        continue
    group_snmp = match_snmp.groupdict()
    community = group_snmp["community"]
    snmp_dict["community"] = community
    group_access = group_snmp["group_access"]
    snmp_dict["group_access"] = group_access
    context = group_snmp["context"]
    snmp_dict["context"] = context
    acl = group_snmp["context"]
    snmp_dict["acl"] = acl
    final_dict[i] = snmp_dict
    i += 1

接下来,由于aclcontext可能会出现空列,所以模式中的\S+匹配将失败。更新模式如下:

代码语言:javascript
复制
show_snmp_community_regex = "^(?P<community>\S+)\s+(?P<group_access>\S+)\s*(?P<context>\S*)\s*(?P<acl>\S*)$"

在本地测试时的输出:

代码语言:javascript
复制
┌─[hjpotter92]─[Oct 29, 2015]─[Programming]
└──$ temp.py
{ 0: { 'acl': 'C0n_text!',
       'community': 'Community_test-1',
       'context': 'C0n_text!',
       'group_access': 'network-operator'},
  1: { 'acl': '',
       'community': 'WEWORK',
       'context': '',
       'group_access': 'network-operator'},
  2: { 'acl': '_C0n',
       'community': 'RW',
       'context': '_C0n',
       'group_access': 'network-admin'},
  3: { 'acl': 'my_acl',
       'community': 'YANKS',
       'context': 'my_acl',
       'group_access': 'network-admin'}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33409453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档