Regex to extract multiple fields in the same line
Ganesh Pal
ganesh1pal at gmail.com
Wed Jun 13 04:08:00 EDT 2018
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'
Expected output
data_block
/var/1000111/test18.log
0
8
Here is my sample code , its still not complete , I wanted to use regex
and find and extract all the fields after " =", any suggestion or
alternative way to optimize this further
import re
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'
r_loc = r"--loc=(\d+)"
r_struct = r'--struct=(\w+)'
if re.findall(r_loc, line):
print re.findall(r_loc, line)
if re.findall(r_struct, line):
print re.findall(r_struct, line)
root at X1:/# python regex_02.py
['0']
['data_block']
I am a Linux user with python 2.7
Regards,
Ganesh
More information about the Python-list
mailing list