简体   繁体   中英

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. The only problem I have is that all these solution seem to match "too much". For example, the match "17" in "MH17". 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

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