Home > Net >  Python Regex - Pattern Matching
Python Regex - Pattern Matching

Time:09-01

I am looking for a way to regex unneeded information in a txt file. The .txt file is constantly changing because it is being generated with a paramiko script that pulls specific data from a network device.

The file contains the following:

*Flags: X - disabled, R - running 
 0  R name="" mtu= l2mtu= mac-address= 
      arp=enabled interface-type= mode=station 
      ssid="" frequency= band=5ghz-a/n 
      channel-width=20/40mhz-XX secondary-frequency="" scan-list=5000-5900 
      wireless-protocol= vlan-mode=no-tag vlan-id=1 wds-mode= 
      wds-default-bridge=none wds-ignore-ssid=no bridge-mode= 
      default-authentication= default-forwarding= default-ap-tx-limit= 
      default-client-tx-limit= hide-ssid= security-profile= 
      compression=* 

The specific part I am interested in is the scan-list=xxxx-xxxx

I accomplished this at first using linux and calling a .sh script with subprocess that would grep the required part, unfortunately, now I have to implement it on a windows system and do not have access to grep.

The code I currently have is able to return scan-list but also returns random brackets, here is the code and output:

Code:

import re
import string

file = open('test.txt', 'r')

for lines in file:
    matches = re.match('..........\d\d\d\d.\d\d\d\d', lines)
    print(matches)

**Output:**

    []
    []
    []
    []
    []
    []
    []
    []
    ['scan-list=5000-5900']
    []
    []
    []
    []
    []
    []
    []
    []
    []
    []
    []
    []
    []

This is all great but I cannot use this output as input for another function due to the brackets.

I have tried the .strip() but get stuck with a TypeError: expected string or bytes-like object presumably because the content is in a file and not a specified string.

I have also tried calling re.sub using lambda but this seems to only return the brackets and not the scan-list.

Eventually to try and bypass the problem I attempted to write the output to a secondary temporary file and do another regex with re .sub / re .replace to remove the brackets, but this ended with the regex once again keeping all the brackets but removing the scan-list part.

After all of this failed, I decided to go back and look at the regex itself, even doing a pattern match for "scan-list\d\d\d\d-\d\d\d\d" seemed to fail and remove everything except the brackets.

I am now wondering if these brackets are cursed and I will just have to resort to WSL. Is there perhaps an easier way to go about this or a different library I can check out and try?

This is probably a very simple problem, and I believe my regex string might be terrible or completely incorrect.

CodePudding user response:

import re
import string

file = open('test2.txt', 'r')

for lines in file:
    matches = re.search('scan-list=\d{4}-\d{4}', lines)
    if matches != None:
      print(matches.group())
  • Related