Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何用正则表达式匹配缺少的值_Python_Regex_Parsing_Missing Data - Fatal编程技术网

Python 如何用正则表达式匹配缺少的值

Python 如何用正则表达式匹配缺少的值,python,regex,parsing,missing-data,Python,Regex,Parsing,Missing Data,我有一个表(本例中为字符串),如下所示: Community Group / Access context acl_filter --------- -------------- ------- ---------- Community_test-1 network-operator C0n_text! T3st-ACL#$ WEWORK network-operator

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

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
我必须在不使用额外代码的情况下使用单个正则表达式来匹配和解析所有值,但是我在编写匹配缺失值的正则表达式时也遇到了问题

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

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)

我用不同的正则表达式尝试了许多方法,但都没有成功:(

出现错误是因为,当您将原始字符串拆分为
show\u snmp\u community\u split
时,第一个元素是空字符串:
'
。当与
re.match
一起使用时,返回
None

在循环中使用if语句:

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
接下来,由于
acl
context
可能存在空列,因此模式中的
\S+
匹配将失败。请将模式更新为:

show_snmp_community_regex = "^(?P<community>\S+)\s+(?P<group_access>\S+)\s*(?P<context>\S*)\s*(?P<acl>\S*)$"

引发此错误的原因是,当您将原始字符串拆分为
show\u snmp\u community\u split
时,第一个元素是一个空白字符串:
'
。当与
re.match
一起使用时,该元素返回
None

在循环中使用if语句:

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
接下来,由于
acl
context
可能存在空列,因此模式中的
\S+
匹配将失败。请将模式更新为:

show_snmp_community_regex = "^(?P<community>\S+)\s+(?P<group_access>\S+)\s*(?P<context>\S*)\s*(?P<acl>\S*)$"

(?P\S+)
typo?
(?P\S+)
typo?感谢您的回复…这样我就获得了字典条目2和3的重复值,如:2:{'acl':'u C0n','community':'RW','context':'u C0n','group\u access':'network admin},这是因为regex无法知道您在不同列中使用了多少空格。那么,我是在问一些无法实现的问题吗?@Gabry_Jin这是可能的,但不仅仅是使用regex。感谢您的回复…这样做,我会获得字典条目2和3的重复值,如:2:{'acl':'u C0n','community':'RW','context':'u C0n','group_access':'network admin'},这是因为正则表达式无法知道不同的列使用了多少空格。所以,我是在问一些无法实现的问题吗?@Gabry_Jin这是可能的,但不能仅使用正则表达式。
┌─[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'}}