Regex to extract multiple fields in the same line

Rhodri James rhodri at kynesim.co.uk
Wed Jun 13 08:29:53 EDT 2018


On 13/06/18 09:08, Ganesh Pal wrote:
>   Hi Team,
> 
> I wanted to parse a file and extract few feilds that are present after "="
> in a text file .
> 
> 
> Example , form  the below line I need to extract the values present after
> --struct =, --loc=, --size= and --log_file=
> 
> Sample input
> 
> line = '06/12/2018 11:13:23 AM python toolname.py  --struct=data_block
> --log_file=/var/1000111/test18.log --addr=None --loc=0 --mirror=10
> --path=/tmp/data_block.txt size=8'

Did you mean "--size=8" at the end?  That's what your explanation implied.

> 
> Expected output
> 
> data_block
> /var/1000111/test18.log
> 0
> 8

Regexs are more complicated than you need for this.  Plain split will do.

line = '06/12/2018 11:13:23 AM python toolname.py  --struct=data_block
  --log_file=/var/1000111/test18.log --addr=None --loc=0 --mirror=10
  --path=/tmp/data_block.txt --size=8'

for item in line.split():
     if item.startswith("--struct="):
         # You may want to tidy this up
         print(item[9:])
     # ...and so on for the other fields

Alternatively you could observe this is a previously solved problem and 
abuse argparse:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("prefix", nargs=5)
parser.add_argument("--struct")
parser.add_argument("--log_file")
parser.add_argument("--addr")
parser.add_argument("--loc")
parser.add_argument("--mirror")
parser.add_argument("--path")
parser.add_argument("--size")

line = '06/12/2018 11:13:23 AM python toolname.py --struct=data_block 
--log_file=/var/1000111/test18.log --addr=None --loc=0 --mirror=10 
--path=/tmp/data_block.txt --size=8'

args = parser.parse_args(line.split())
print(args)


-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list