Find multiple regex in each line and skip result if one of the regex doesn't match
- by williamx
I have a list of variables:
variables = ['VariableA', 'VariableB','VariableC']
which I'm going to search for, line by line
ifile = open("temp.txt",'r')
d = {}
match = zeros(len(variables))
for line in ifile:
emptyCells=0
for i in range(len(variables)):
regex = r'('+variables[i]+r')[:|=|\(](-?\d+(?:\.\d+)?)(?:\))?'
pattern_variable = re.compile(regex)
match[i] = re.findall(pattern_variable, line)
if match[j] == []:
emptyCells = emptyCells+1
if emptyCells == 0:
for k, v in match[j]:
d.setdefault(k, []).append(v)
The requirement is that I will only keep the lines where all the regex'es matches!
I want to collect all results for each variable in a dictionary where the variable name is the key, and the value becomes a list of all matches.
The code provided is only what I've found out so far, and is not working perfectly yet...