jaydh
jaydh

Reputation: 111

Use of re.compile and groups in a regular expression in python

The input file, named consensus, is of the following form:

r Tor4ever AAcif1htILdru0BO0qX7OwGVhAU oHlbWBdaN3+QSleqBVL9/yAdcRs 2014-07-31 21:42:43  
s Exit Fast Guard HSDir Running V2Dir Valid  
v Tor 0.2.4.21  
w Bandwidth=231  
p reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999  
r Tornin AA8YrCza5McQugiY3J4h5y4BF9g vNRd1kyQ0i9UsVwYq5YFPHJi3jw 2014-08-01 00:26:18  
s Fast Guard HSDir Named Running Stable V2Dir Valid  
v Tor 0.2.4.23  
w Bandwidth=713  
p reject 1-65535

I want to parse out the name (beginning of the r lines), guard flag (in the s lines) and bandwidth (in the w lines) to give something like the following:

{ {"nickname" : "Tor4ever", "type" : "Guard", "bandwidth" : "231"}, 
{"nickname" : "Tornin", "type" : "Guard", "bandwidth" : "713"} }  

I'm having trouble formulating the correct regex. Here is the relevant part of the code I'm using:

consensus = file(sys.argv[1]).read() 

regex = re.compile('''^r\s(.*?)\s.*?\ns\s.*?(Guard)\s.*?\nw\s.*?([0-9]+)\n''',  
re.MULTILINE)

for record in regex.finditer(consensus):
    relay = {
    'nickname' : record.group(1),
    'type' : record.group(2),
    'bandwidth' : record.group(3),
    }

    relays['relays'].append(relay)

open('tor_relays.txt','w').write(json.dumps(relays, indent=4))

Can someone tell me why my regular expression is not parsing the way I expected it to? Sorry for the long post and thanks in advance!

Upvotes: 0

Views: 1035

Answers (1)

OnlineCop
OnlineCop

Reputation: 4069

Try

^r\s(?P<nickname>\b\S+\b).*\n^s\b.*?(?P<type>\bGuard\b).*\n^v.*\n^w\s.*?(?P<bandwidth>\b[0-9]+\b)

It may be a little more verbose than you need, but it makes it a little easier to read. Regex101 demo

import re
p = re.compile(ur'^r\s(?P<nickname>\b\S+\b).*\n^s\b.*?(?P<type>\bGuard\b).*\n^v.*\n^w\s.*?(?P<bandwidth>\b[0-9]+\b)', re.MULTILINE | re.VERBOSE)
test_str = u"r Tor4ever AAcif1htILdru0BO0qX7OwGVhAU oHlbWBdaN3+QSleqBVL9/yAdcRs 2014-07-31 21:42:43  \ns Exit Fast Guard HSDir Running V2Dir Valid  \nv Tor 0.2.4.21  \nw Bandwidth=231  \np reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999  \nr Tornin AA8YrCza5McQugiY3J4h5y4BF9g vNRd1kyQ0i9UsVwYq5YFPHJi3jw 2014-08-01 00:26:18  \ns Fast Guard HSDir Named Running Stable V2Dir Valid  \nv Tor 0.2.4.23  \nw Bandwidth=713  \np reject 1-65535"

re.findall(p, test_str)

Upvotes: 1

Related Questions