-1

I am trying to understand what this regex([0.00-9.00]+) in python does. The way I understand this is it would match any of 0 . 0 0-9 . 0 0, so the zeroes are redundant here. Since there is a . inside it should match any character and the complete match should be a non-empty sequence of characters not necessarily digits, but it doesn't behave that way. Am I missing anything?

Few examples of how it behaves

1 Answers1

6

The zeroes are indeed redundant. The . only matches a literal . because it is part of a character class, the [...] syntax. It has no special meaning there.

So the character class could be reduced to:

[0-9.]+

and still match the exact same inputs.

The author of that regex text seems to have confused the - feature in a character class with a numeric range feature; there is no such thing in a regex. You'd have to match individual characters that in aggregate can be read as real numbers. The following would actually match such numbers:

(?<!\d)\d\.\d\d(?!\d)

where the negative look-behind and look-ahead make sure there are not more digits surrounding this text. \d is shorthand for [0-9] here.

See this regex101 demo.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997