简体   繁体   中英

Regex to match both float and scientific notation

I'm trying to match strings that look something like:

{"1": [123, 456, 789], "time": 1.234e-05}

or

{"1": [123, 456, 789], "time": 1.234}

Here is what I have:

\\{"1": \\[123, 456, 789], "time": [0-9.]*}

While this Regex matches the second string that has a float on time , it would fail to catch scientific notation with e-... . How should I change my Regex to match both?

[0-9.]*(e\\-[0-9]*)?

.e-匹配列表中的单个字符.e- (区分大小写)

You can use one or zero quantifier ? :

>>> re.match('(\d+\.\d+(e-\d+)?)', '1.234').group(1)
'1.234'
>>> re.match('(\d+\.\d+(e-\d+)?)', '1.234e-05').group(1)
'1.234e-05'

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