2

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

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

Arun
  • 1,149
  • 11
  • 22
  • 1
    Split the text found using split iwith "#" and keep the first part of it. – Rao Sahab Apr 06 '18 at 06:38
  • 2
    It can be , but I believe it can solve by the regex itself. if it is possible I no need to write another line of code to split it. – Arun Apr 06 '18 at 06:40
  • Regex is a complex tool. I suggest you study it thoroughly before attempting to use it. Also regex is not very efficient, so maybe you should consider just using "split" as @Rao mentions. – Holger Bille Apr 06 '18 at 07:02

5 Answers5

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.

Jonathan
  • 748
  • 3
  • 20
1

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

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']
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
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.

It looks as if this is an INI file.

Example Code

Example INI file

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

[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:

#!/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.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
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
Veera Balla Deva
  • 790
  • 6
  • 19
  • 1
    You should improve your answer and add an explanation, what your code does and why this solves the problem of the OP. – Mr. T Apr 06 '18 at 07:36
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Isma Apr 06 '18 at 07:54
0

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

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
Holger Bille
  • 2,421
  • 1
  • 16
  • 20