I am using python to search through a text log file line by line and I want to save a certain part of a line as a variable. I am using Regex but don't think I am using it correctly as I am always get None for my variable string_I_want. I was looking at other Regex questions on here and saw people adding .group() to the end of their re.search but that gives me an error. I am not the most familiar with Regex but can't figure out where am I going wrong?
Sample log file:
2016-03-08 11:23:25 test_data:0317: m=string_I_want max_count: 17655, avg_size: 320, avg_rate: 165
My script:
def get_data(log_file):
#Read file line by line
with open(log_file) as f:
f = f.readlines()
for line in f:
date = line[0:10]
time = line[11:19]
string_I_want=re.search(r'/m=\w*/g',line)
print date, time, string_I_want
refunctions and methods do --- read the "Regular Expression HOWTO" for a thorough introduction to using regular expressions in Python 2, and refer to therereference docs when you need to look up specifics. It will save you time in the long run.