简体   繁体   中英

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)   

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM