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

时间:2021-09-20 00:32:21

I have config file which contains network configurations something like given below.

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

LISTEN=192.168.180.1 #the network which listen the traffic 
NETMASK=255.255.0.0
DOMAIN =test.com

Need to grep the values from the config. the following is my current code.

需要从配置中提取值。下面是我当前的代码。

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

the variable listen contains

该变量包含听

192.168.180.1 #the network which listen the traffic

192.168.180.1 #监听通信的网络

but I no need the commented information but sometimes comments may not exist like other "NETMASK"

但是我不需要注释信息但是有时候注释可能不像其他的“NETMASK”那样存在

5 个解决方案

#1


1  

If you really want to this using regular expressions I would suggest changing it to LISTEN=([^#$]+)

如果你真的想使用正则表达式我建议改变它听=((^ # $)+)

Which should match anything up to the pound sign opening the comment or a newline character.

它应该与注释开头的英镑符号或换行字符匹配。

#2


1  

I come up with solution which will have common regex and replace "#".

我提出的解决方案将具有公共regex并替换“#”。

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

Output :

输出:

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']

#3


0  

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

#4


0  

In my experience regex is slow runtime and not very readable. I would do:

根据我的经验,regex运行缓慢,可读性不强。我要做:

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

#5


0  

I think the better approach is to read the whole file as the format it is given in. I wrote a couple of tutorials, e.g. for YAML, CSV, JSON.

我认为更好的方法是按照文件的格式来读取整个文件。我写了一些教程,比如YAML, CSV, JSON。

It looks as if this is an INI file.

看起来这是一个INI文件。

Example Code

Example INI file

INI files need a header. I assume it is network:

INI文件需要一个标题。我认为是网络:

[network]
LISTEN=192.168.180.1 #the network which listen the traffic 
NETMASK=255.255.0.0
DOMAIN =test.com

Python 2

#!/usr/bin/env python

import ConfigParser
import io

# Load the configuration file
with open("config.ini") as f:
    sample_config = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))

# List all contents
print("List all contents")
for section in config.sections():
    print("Section: %s" % section)
    for options in config.options(section):
        print("x %s:::%s:::%s" % (options,
                                  config.get(section, options),
                                  str(type(options))))

# Print some contents
print("\nPrint some contents")
print(config.get('other', 'use_anonymous'))  # Just get the value

Python 3

Look at configparser:

看看configparser:

#!/usr/bin/env python

import configparser

# Load the configuration file
config = configparser.RawConfigParser(allow_no_value=True)
with open("config.ini") as f:
    config.readfp(f)

# Print some contents
print(config.get('network', 'LISTEN'))

gives:

给:

192.168.180.1 #the network which listen the traffic

Hence you need to parse that value as well, as INI seems not to know #-comments.

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

#1


1  

If you really want to this using regular expressions I would suggest changing it to LISTEN=([^#$]+)

如果你真的想使用正则表达式我建议改变它听=((^ # $)+)

Which should match anything up to the pound sign opening the comment or a newline character.

它应该与注释开头的英镑符号或换行字符匹配。

#2


1  

I come up with solution which will have common regex and replace "#".

我提出的解决方案将具有公共regex并替换“#”。

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

Output :

输出:

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']

#3


0  

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

#4


0  

In my experience regex is slow runtime and not very readable. I would do:

根据我的经验,regex运行缓慢,可读性不强。我要做:

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

#5


0  

I think the better approach is to read the whole file as the format it is given in. I wrote a couple of tutorials, e.g. for YAML, CSV, JSON.

我认为更好的方法是按照文件的格式来读取整个文件。我写了一些教程,比如YAML, CSV, JSON。

It looks as if this is an INI file.

看起来这是一个INI文件。

Example Code

Example INI file

INI files need a header. I assume it is network:

INI文件需要一个标题。我认为是网络:

[network]
LISTEN=192.168.180.1 #the network which listen the traffic 
NETMASK=255.255.0.0
DOMAIN =test.com

Python 2

#!/usr/bin/env python

import ConfigParser
import io

# Load the configuration file
with open("config.ini") as f:
    sample_config = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))

# List all contents
print("List all contents")
for section in config.sections():
    print("Section: %s" % section)
    for options in config.options(section):
        print("x %s:::%s:::%s" % (options,
                                  config.get(section, options),
                                  str(type(options))))

# Print some contents
print("\nPrint some contents")
print(config.get('other', 'use_anonymous'))  # Just get the value

Python 3

Look at configparser:

看看configparser:

#!/usr/bin/env python

import configparser

# Load the configuration file
config = configparser.RawConfigParser(allow_no_value=True)
with open("config.ini") as f:
    config.readfp(f)

# Print some contents
print(config.get('network', 'LISTEN'))

gives:

给:

192.168.180.1 #the network which listen the traffic

Hence you need to parse that value as well, as INI seems not to know #-comments.

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