I'm just learning regex and now I'm trying to match a number which more or less represents this:
[zero or more numbers][possibly a dot or comma][zero or more numbers]
No dot or comma is also okay. So it should match the following:
1
123
123.
123.4
123.456
.456
123, # From here it's the same but with commas instead of dot separators
123,4
123,456
,456
But it should not match the following:
0.,1
0a,1
0..1
1.1.2
100,000.99 # I know this and the one below are valid in many languages, but I simply want to reject these
100.000,99
So far I've come up with [0-9]*[.,][0-9]*
, but it doesn't seem to work so well:
>>> import re
>>> r = re.compile("[0-9]*[.,][0-9]*")
>>> if r.match('0.1.'): print 'it matches!'
...
it matches!
>>> if r.match('0.abc'): print 'it matches!'
...
it matches!
I have the feeling I'm doing two things wrong: I don't use match correctly AND my regex is not correct. Could anybody enlighten me on what I'm doing wrong? All tips are welcome!
The problem is that you are asking for a partial match, as long as it starts at the beginning.
One way around this is to end the regex in
\Z
(optionally$
).and the other is to use
re.fullmatch
instead.vs
Note that
fullmatch
is new in 3.4.You should also make the
[.,]
part optional, so append a?
to that.Eg.
More generic method can be as follows
This will match the following pattern:
This will not match the following pattern: