简体   繁体   English

正则表达式匹配浮点数和科学记数法

[英]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-... .虽然此正则表达式匹配第二个具有浮点time字符串,但它无法捕捉带有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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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