Christian
Christian

Reputation: 3393

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?

Upvotes: 1

Views: 133

Answers (1)

HMK
HMK

Reputation: 574

I rewrite the original regex: fix +/- bug and exclude the case starting with letter

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

Also please find the demo

Upvotes: 2

Related Questions