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

Bog*_*scu 1 python regex

我正在尝试匹配科学记数法中的数字(来自此处的正则表达式):

scinot = re.compile('[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)')
re.findall(scinot, 'x = 1e4')
['1e4']
re.findall(scinot, 'x = c1e4')
['1e4']
Run Code Online (Sandbox Code Playgroud)

我希望它匹配x = 1e4但不匹配x = c1e4。我应该改变什么?

更新:这里的答案有同样的问题:它错误地匹配'x = c1e4'.

Tot*_*oto 5

在正则表达式末尾添加锚点,并在数字前添加替代空格或等号:

[\s=]+([+-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+))$
Run Code Online (Sandbox Code Playgroud)