Rob
Rob

Reputation: 584

Python regex match certain floating point numbers

I'm trying to match: 0 or more numbers followed by a dot followed by ( (0 or more numbers) but not (if followed by a d,D, or _))

Some examples and what should match/not:

match:

['1.0','1.','0.1','.1','1.2345']

not match:

['1d2','1.2d3','1._dp','1.0_dp','1.123165d0','1.132_dp','1D5','1.2356D6']

Currently i have:

"([0-9]*\.)([0-9]*(?!(d|D|_)))"

Which correctly matches everything in the match list. But for those in the things it should not match it incorrectly matches on:

['1.2d3','1.0_dp','1.123165d0','1.132_dp','1.2356D6']

and correctly does not match on:

['1d2','1._dp','1D5']

So it appears i have problem with the ([0-9]*(?!(d|D|_)) part which is trying to not match if there is a d|D|_ after the dot (with zero or more numbers in-between). Any suggestions?

Upvotes: 1

Views: 56

Answers (2)

The fourth bird
The fourth bird

Reputation: 163287

Instead of using a negative lookahead, you might use a negated character class to match any character that is not in the character class.

If you only want to match word characters without the dD_ or a whitespace char you could use [^\W_Dd\s].

You might also remove the \W and \s to match all except dD_

^[0-9]*\.[^\W_Dd\s]*$

Explanation

  • ^ Start of string
  • [0-9]*\. Match 0+ times a digit 0-9 followed by a dot
  • [^\W_Dd\s]* Negated character class, match 0+ times a word character without _ D d or whitespace char
  • $ End of string

Regex demo

If you don't want to use anchors to assert the start and the end of the string you could also use lookarounds to assert what is on the left and right is not a non whitspace char:

(?<!\S)[0-9]*\.[^\W_Dd\s]*(?!\S)

Regex demo

Upvotes: 2

Onyambu
Onyambu

Reputation: 79208

\d*[.](?!.*[_Dd]).* is what you are looking for:

Upvotes: 1

Related Questions