Find multiple regex in each line and skip result if one of the regex doesn't match
Posted
by williamx
on Stack Overflow
See other posts from Stack Overflow
or by williamx
Published on 2010-03-22T11:27:30Z
Indexed on
2010/03/22
11:31 UTC
Read the original article
Hit count: 1607
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...
© Stack Overflow or respective owner