简体   繁体   English

Python:使用正则表达式从字符串中提取数字

[英]Python: extracting numbers from string using regex

I'm having a string and trying to find all numbers such as 1, -1.5, .5, etc. I already found this question with very helpful answers. 我有一个字符串,试图找到所有数字,例如1,-1.5,.5等。我已经找到了这个问题,并且给出了非常有用的答案。 The only problem I have is that all these solution seem to match "too much". 我唯一的问题是所有这些解决方案似乎都“太多”了。 For example, the match "17" in "MH17". 例如,“ MH17”中的匹配“ 17”。 How can I extend any of the proposed solution so that a number cannot start with (or contain) a letter? 如何扩展任何建议的解决方案,使数字不能以字母开头(或包含字母)?

I rewrite the original regex: fix +/- bug and exclude the case starting with letter 我重写了原始的正则表达式:修复了+/-错误并排除了以字母开头的情况

(?<=\s)[+-]?\d+(?:.\d)?\d*

Also please find the demo 还请找到演示

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

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