Hi,
I try to read line by line from a file some data. Each line in that file has the following format:
x = 78.36734693877551, z = 11.428571428571429 -> amplitude: 8.62847057093655E-6 | phase -1.5707968246742405\n
I am trying to extract those four float numbers using regular expression. I came with the following code:
import re myteststr='x = 78.36734693877551, z = 11.428571428571429 -> amplitude: 8.62847057093655E-6 | phase -1.5707968246742405\n' rs=re.compile(r'^x = (\S+), z = (\S+) -> amplitude: (\S+) | phase (\S+)(\s*)$') rss=rs.search(myteststr).groups()
The problem is that only the first 3 floats are extracted. The last one and the '\n' character aren't extracted - they are set to the None object:
>>> rss
('78.36734693877551', '11.428571428571429', '8.62847057093655E-6', None, None)Please tell me where I do wrong. I want to be able to extract the last number also.
Best regards,
PTS
The vertical bar is a special character in re. You can use
rs=re.compile(r'^x = (\S+), z = (\S+) -> amplitude: (\S+) \| phase (\S+)(?:\s*)$')
Also this looks nice http://stackoverflow.com/questions/385558/python-and-regex-question-extract-float-double-value