I have a problem with python regex. The result seems fine on regxe buddy, but fails in python.
Set of data i have to match is a list:
[' 101 0. 0.\n',
' 0. 100.\n',
' 1. 98.5107805\n',
' 2. 97.0464459\n',
' 3. 95.6065328\n', ... ]
I have to get all the numbers starting from second line. For this i used:
pattern = compile(r'\s*(?P<raw_time>\d*\.?\d*)\s+(?P<raw_value>\d*\.\d*)')
And all worked fine. I iterate through the list and get the first value in "raw_time" and second one in "raw_value" for every line. Then i was supposed to expand the term to also work with weighted data.
So the data turned to:
[' 101 0. 0.\n',
' 0. 100. 1\n',
' 1. 98.5107805 1\n',
' 2. 97.0464459 1\n',
' 3. 95.6065328 1\n', ... ]
I still just have to parse out two first parameters. So i changed the pattern to:
pattern = compile(r'\s*(?P<raw_time>\d*\.?\d*)\s+(?P<raw_value>\d*\.\d*).+')
It works fine all lines except the 1.
Working:
In [35]: pattern.search('1. 98.5107805 1\n').groupdict()
Out[35]: {'raw_time': '1.', 'raw_value': '98.5107805'}
Working:
In [37]: pattern.search(' 0. 100. 1\n').groupdict()
Out[37]: {'raw_time': '0.', 'raw_value': '100.'}
Working:
In [44]: pattern.search('1. 98.5107805\n').groupdict() Out[44]: {'raw_time': '1.', 'raw_value': '98.510780'}
Not working:
In [46]: pattern.search(' 0. 100.\n').groupdict()
Out[46]: {'raw_time': '', 'raw_value': '0.'}
I heavily rely on regex (sure, demo, but it was consistent with python till now).
Advice?
tnx