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?
Asked
Active
Viewed 129 times
-1

Abinash Meher
- 104
- 8
-
1No, `.` in a character range does not match 'any' character, only a literal dot. – Martijn Pieters Oct 11 '16 at 06:52
-
1https://regex101.com/r/BSZI6b/1 – deceze Oct 11 '16 at 06:53
-
1Also, see http://stackoverflow.com/questions/19976018/does-a-dot-have-to-be-escaped-in-a-character-class-square-brackets-of-a-regula and http://stackoverflow.com/questions/21929693/decyphering-a-simple-regex/21929764#21929764 – Wiktor Stribiżew Oct 11 '16 at 07:02
1 Answers
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
- 1,048,767
- 296
- 4,058
- 3,343