简体   繁体   中英

Regex scientific notation

I need to float the following strings that represent scientific notation values without the exponent:

'-1.366-6'
'1.366-6'
'-1.366+6'
'1.366+6'
'-1.25-10'

I need to insert an 'e' before the final + or -

'-1.366e-6'
'1.366e-6'
'-1.366e+6'
'1.366e+6'
'-1.25e-10'

How can I use regex to achieve this?

You need to use re.sub ,

re.sub(r'(?=[-+][^-+]*$)', 'e', string)

The above regex matches the boundary which was followed by last + or - character.

DEMO

Example:

>>> import re
>>> l = ['-1.366-6', '1.366-6', '-1.366+6', '1.366+6', '-1.25-10']
>>> for i in l:
        print(re.sub(r'(?=[-+][^-+]*$)', 'e', i))


-1.366e-6
1.366e-6
-1.366e+6
1.366e+6
-1.25e-10

Note that

re.sub(r'(?=[-+][^-+]*$)', 'e', '+1.3666')

returns 'e+1.3666' . If you'd like it to return '+1.3666' , then you could use

re.sub(r'(?<=\d)(?=[+-])', 'e', text)

instead.


import re

tests = [
('-1.366-6', '-1.366e-6'),
('1.366-6',  '1.366e-6' ),
('-1.366+6', '-1.366e+6'),
('1.366+6',  '1.366e+6' ),
('-1.25-10', '-1.25e-10'),
('+1.366', '+1.366'),
]

for text, expected in tests:
    result = re.sub(r'(?<=\d)(?=[+-])', 'e', text)
    print(result)
    assert result == expected

yields

-1.366e-6
1.366e-6
-1.366e+6
1.366e+6
-1.25e-10
+1.366

The pattern (?<=\\d)(?=[+-]) means

(?<=      # match if preceded by 
\d        # a digit
 )    
(?=       # match if followed by
[+-]      # a literal plus or minus sign
 )      

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