Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help in re module of python

Goldy TommyGoldy Tommy Member Posts: 1

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?
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()

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
    
Tagged:
Sign In or Register to comment.