Python multiline regex

I had a problem compiling the correct regular expression for a multi-line match. Can someone point out what I'm doing wrong. I am browsing a basic dhcpd.conf file with hundreds of entries, for example:

host node20007                                                                                                                  
{                                                                                                                              
    hardware ethernet 00:22:38:8f:1f:43;                                                                                       
    fixed-address node20007.domain.com;     
}

I received various regular expressions for working with MAC and fixed address, but I can not combine them to match.

f = open('/etc/dhcp3/dhcpd.conf', 'r')
re_hostinfo = re.compile(r'(hardware ethernet (.*))\;(?:\n|\r|\r\n?)(.*)',re.MULTILINE)

for host in f:
match = re_hostinfo.search(host)
    if match:
        print match.groups()

Currently, my compliance groups will look like this:
('ethernet 00: 22: 38: 8f: 1f: 43', '00: 22: 38: 8f: 1f: 43 ',' ')

But we are looking for something like:
('hardware ethernet 00: 22: 38: 8f: 1f: 43', '00: 22: 38: 8f: 1f: 43 ',' node20007.domain.com ')

+5
2

, ; :

for host in f:
    match = re_hostinfo.search(host)
    if match:
        print match.groups()

host , .

:

data = f.read()
for x in regex.finditer(data):
    process(x.groups())

regex - , .

, , , , , , . , , , , pyparsing.

, , :

MULTILINE; . , :

(1) (2) (3)

, .

:

>>> m = re.search(r'(hardware ethernet\s+(\S+));\s+\S+\s+(\S+);', data)
>>> print m.groups()
('hardware ethernet   00:22:38:8f:1f:43', '00:22:38:8f:1f:43', 'node20007.domain.com')
>>>

, " "... , , , . :

>>> regex = re.compile(r"""
... (hardware[ ]ethernet \s+
...     (\S+) # MAC
... ) ;
... \s+ # includes newline
... \S+ # variable(??) text e.g. "fixed-address"
... \s+
... (\S+) # e.g. "node20007.domain.com"
... ;
... """, re.VERBOSE)
>>> print regex.search(data).groups()
('hardware ethernet   00:22:38:8f:1f:43', '00:22:38:8f:1f:43', 'node20007.domain.com')
>>>
+10

regex.

for line in open("dhcpd.conf"):
    line = line.rstrip()
    sline = line.split()
    if "hardware ethernet" or "fixed-address" in line:
       print sline[-1]

data = open("file").read().split("}");
for item in data:
    item = [ i.strip() for i in item.split("\n") if i != '' ];
    for elem in item:
       if "hardware ethernet" in elem:
           print elem.split()[-1]
    if item: print  item[-1]

$ more file
host node20007
{
    hardware ethernet 00:22:38:8f:1f:43;
        fixed-address node20007.domain.com;
}

host node20008
{
    hardware ethernet 00:22:38:8f:1f:44;
        some-address node20008.domain.com;
}

$ python test.py
00:22:38:8f:1f:43;
fixed-address node20007.domain.com;
00:22:38:8f:1f:44;
some-address node20008.domain.com;
0

All Articles