Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to use the re module to parse through a file. I tired three version of the code, first 2 version's are not retrieving any O/P. The third version is retrieving only one line. Can someone please have a look?

The data in sample.txt is as below. The network is blank after first line, and when I'm running individually these statements on python shell the regex is working.
**********************************************************************

Oregon Exchange BGP Route Viewer
route-views.oregon-ix.net / route-views.routeviews.org

This hardware is part of a grant by the NSF.
Please contact help@routeviews.org if you have questions, or
if you wish to contribute your view.

Network Next Hop Metric . LocPrf Weight Path
* 64.48.0.0/16 173.205.57.234 0 53364 3257 2828 i

* 202.232.0.2 0 2497 2828 i

* 93.104.209.174 0 58901 51167 1299 2828 i

* 193.0.0.56 0 3333 2828 i

* 103.197.104.1 0 134708 3491 2828 i

* 132.198.255.253 0 1351 6939 2828 i

What I have tried:

Version1:

import re
file = open('sample.txt', 'r')
x = file.readline()
while x:
var = re.findall(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x)
x = file.readline()
print(var)
file.close()

Version2:

import re
file = open('sample.txt', 'r')
x = file.read()
var = re.findall(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x)
print(var)
file.close()
Version3:

import re
file = open('sample.txt', 'r')
x = file.readline()
while x:
var = re.search(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x, re.M)
x = file.readline()
print(var.group(0))
file.close()
Posted
Updated 13-Jul-17 4:56am

1 solution

I just ran your code and it works fine, apart from listing the blank entries:
[]
[]
[]
[]
[]
[]
[]
[]
[('64.48.0.0/16 ', '173.205.57.234', '53364 3257 2828 ')]
[]
[('', '202.232.0.2', '2497 2828 ')]
[]
[('', '93.104.209.174', '58901 51167 1299 2828 ')]
[]
[('', '193.0.0.56', '3333 2828 ')]
[]
[('', '103.197.104.1', '134708 3491 2828 ')]
[]
[('', '132.198.255.253', '1351 6939 2828 ')]


It would help if you put your code within <pre> tags thus:
Python
import re
file = open('sample.txt', 'r') 
x = file.readline()
while x:
    var = re.findall(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x)
    x = file.readline()
    print(var)
file.close()


so we can see whether your indentations are correct.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900