正则表达式匹配科学记数法 [英] Regex to match scientific notation
本文介绍了正则表达式匹配科学记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以科学记数法匹配数字(来自此处的正则表达式):
I'm trying to match numbers in scientific notation (regex from here):
scinot = re.compile('[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)')
re.findall(scinot, 'x = 1e4')
['1e4']
re.findall(scinot, 'x = c1e4')
['1e4']
我希望它匹配 x = 1e4
但不匹配 x = c1e4
.我应该改变什么?
I'd like it to match x = 1e4
but not x = c1e4
. What should I change?
更新:此处的答案相同问题:它错误地匹配 'x = c1e4'
.
Update: The answer here has the same problem: it incorrectly matches 'x = c1e4'
.
推荐答案
在正则表达式末尾添加锚点并在数字前添加替代空格或等号:
Add anchor at the end of regex and alternative space or equal sign before the number:
[\s=]+([+-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+))$
这篇关于正则表达式匹配科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文