user5568235
user5568235

Reputation:

Python: re.search not working

This is my example. I am trying to search but nothing is printing on screen.

DeltaE is group 1
TDMI^2 is group 2
Intensity is group 3

#   DeltaE =     0.0000 | TDMI^2 =  5.657    , Intensity = 0.5604E+06
    match = re.search(r"DeltaE =\s+(\S+).* TDMI^2 =\s+(\S+).* Intensity =\s+(\S+)", line)   

Upvotes: 1

Views: 132

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174796

Note that ^ is a special char in regex, you must escape it in-order to match a literal carret symbol.

re.search(r"DeltaE =\s+(\S+).* TDMI\^2 =\s+(\S+).* Intensity =\s+(\S+)", line)

Example:

>>> s = "DeltaE = 0.0000 | TDMI^2 = 5.657 , Intensity = 0.5604E+06"
>>> m = re.search(r"DeltaE =\s+(\S+).* TDMI\^2 =\s+(\S+).* Intensity =\s+(\S+)", s)
>>> m.group(1)
'0.0000'
>>> m.group(2)
'5.657'
>>> m.group(3)
'0.5604E+06'
>>> float(m.group(2))
5.657
>>> float(m.group(3))
560400.0
>>> float(m.group(1))
0.0

Upvotes: 3

Related Questions