python中配置文件中的正则表达式到grep字符串

python中配置文件中的正则表达式到grep字符串,python,regex,nsregularexpression,Python,Regex,Nsregularexpression,我有一个配置文件,其中包含如下所示的网络配置 LISTEN=192.168.180.1 #the network which listen the traffic NETMASK=255.255.0.0 DOMAIN =test.com 需要从配置中grep这些值。以下是我当前的代码 import re with open('config.txt') as f: data = f.read() listen = re.findall('LISTEN=(.*)',da

我有一个配置文件,其中包含如下所示的网络配置

LISTEN=192.168.180.1 #the network which listen the traffic 
NETMASK=255.255.0.0
DOMAIN =test.com
需要从配置中grep这些值。以下是我当前的代码

import re
with open('config.txt') as f:
      data = f.read()
      listen =  re.findall('LISTEN=(.*)',data)
      print listen
变量listen包含

192.168.180.1#监听流量的网络


但我不需要注释信息,但有时注释可能不像其他“网络掩码”那样存在。如果您真的想使用正则表达式进行此操作,我建议将其更改为
LISTEN=([^#$]+)


它应该与打开注释的井号或换行符匹配。

我认为更好的方法是按照给定的格式读取整个文件。我写了几篇教程,例如

看起来好像这是一个INI文件

示例代码 示例INI文件 INI文件需要一个头。我假设它是
网络

[network]
LISTEN=192.168.180.1 #the network which listen the traffic 
NETMASK=255.255.0.0
DOMAIN =test.com
Python 2 Python 3 看看:

给出:

192.168.180.1 #the network which listen the traffic

因此,您也需要解析该值,因为INI似乎不知道
#
-注释。

我提出了一个解决方案,该解决方案将使用公共正则表达式并替换“#”

data = """LISTEN=192.168.180.1 #the network which listen the traffic"""
import re
print(re.search(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}', data).group())
>>>192.168.180.1
print(re.search(r'[0-9]+(?:\.[0-9]+){3}', data).group())
>>>192.168.180.1
输出:

Total match found
['192.168.180.1 #the network which listen the traffic', '255.255.0.0', 'test.com']

Match after removing #
['192.168.180.1', '255.255.0.0', 'test.com']

根据我的经验,正则表达式运行速度慢,可读性差。我会:

with open('config.txt') as f:
    for line in f:
        if not line.startswith("LISTEN="):
            continue
        rest = line.split("=", 1)[1]
        nocomment = rest.split("#", 1)[0]
        print nocomment

使用带“#”的Split iw拆分找到的文本并保留其第一部分。可以,但我相信它可以由正则表达式本身解决。如果可能的话,我不需要再写一行代码来拆分它。Regex是一个复杂的工具。我建议你在使用前仔细研究一下。ReGEX也不是很有效率,所以也许你应该考虑只使用“拆分”作为“@ Rao”。它不适用于没有注释的配置。“代码> Re.FiDALL的输出(‘网掩码=([^×$$]),数据)< /COD>是[’255.255.0.0nNeal.Test.com’),这是错误的。你应该改进你的答案并添加解释,你的代码是做什么的,以及为什么这可以解决OP的问题。虽然这个代码片段可以解决这个问题,但它确实有助于提高你文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
import re
data = '''
LISTEN=192.168.180.1 #the network which listen the traffic
NETMASK=255.255.0.0
DOMAIN =test.com
'''
#Common regex to get all values
match =  re.findall(r'.*=(.*)#*',data)

print "Total match found"
print match

#Remove # part if any
for index,val in enumerate(match):
    if "#" in val:
        val = (val.split("#")[0]).strip()
        match[index] = val

print "Match after removing #"
print match
Total match found
['192.168.180.1 #the network which listen the traffic', '255.255.0.0', 'test.com']

Match after removing #
['192.168.180.1', '255.255.0.0', 'test.com']
with open('config.txt') as f:
    for line in f:
        if not line.startswith("LISTEN="):
            continue
        rest = line.split("=", 1)[1]
        nocomment = rest.split("#", 1)[0]
        print nocomment