Python regex for number with or without decimals using a dot or comma as separator?

kramer65

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!

Avinash Raj

You need to make [.,] part as optional by adding ? after that character class and also don't forget to add anchors. ^ asserts that we are at the start and $ asserts that we are at the end.

^\d*[.,]?\d*$

DEMO

>>> import re
>>> r = re.compile(r"^\d*[.,]?\d*$")
>>> if r.match('0.1.'): print 'it matches!'
... 
>>> if r.match('0.abc'): print 'it matches!'
... 
>>> if r.match('0.'): print 'it matches!'
... 
it matches!

If you don't want to allow a single comma or dot then use a lookahead.

^(?=.*?\d)\d*[.,]?\d*$

DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

From Dev

Extracting a number with dot and comma using regex

From Dev

Format number in R with both comma thousands separator and specified decimals

From Dev

Converting a number from a string to an int with decimals separated by comma or dot

From Dev

Regex for decimal number dot instead of comma (.NET)

From Dev

dot instead of comma as thousand separator

From Dev

How to replace number dot (.) with a comma (,) using java

From Dev

Regex for replacing times with comma to a dot in Python

From Dev

detecting dot or comma in a regex

From Dev

Regex allow dot and comma

From Dev

Replacing dot with comma from a dataframe using Python

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Regex for percentages with or without decimals

From Dev

regex for multiple formats decimals and thousand separator

From Dev

Format integer number with . as thousand separator and no decimals

From Dev

Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA?

From Dev

comma separator using implode not working

From Dev

If decimal value, convert to two decimals AND dot separated value to comma separated

From Dev

SQL Server DTS/SSIS decimals dot instead of comma

From Dev

Use comma instead of dot for decimals when saving as text

From Dev

SQL Server DTS/SSIS decimals dot instead of comma

From Dev

How to change position of data columns using regex in a CSV file using comma as separator?

From Dev

Convert number to string without any formating and using "." as floating point separator

From Dev

Regex everything but number with two decimals

From Dev

Google form regex for numbers with comma as decimal separator

From Dev

Python: Detect number separator symbols and parse into a float without locale

From Dev

Jaxb conversion of number with comma as a decimal separator

From Dev

r ggplot with zeroes and no comma as the big number separator

Related Related

  1. 1

    JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

  2. 2

    Extracting a number with dot and comma using regex

  3. 3

    Format number in R with both comma thousands separator and specified decimals

  4. 4

    Converting a number from a string to an int with decimals separated by comma or dot

  5. 5

    Regex for decimal number dot instead of comma (.NET)

  6. 6

    dot instead of comma as thousand separator

  7. 7

    How to replace number dot (.) with a comma (,) using java

  8. 8

    Regex for replacing times with comma to a dot in Python

  9. 9

    detecting dot or comma in a regex

  10. 10

    Regex allow dot and comma

  11. 11

    Replacing dot with comma from a dataframe using Python

  12. 12

    NumericUpDown: accept both comma and dot as decimal separator

  13. 13

    NumericUpDown: accept both comma and dot as decimal separator

  14. 14

    Regex for percentages with or without decimals

  15. 15

    regex for multiple formats decimals and thousand separator

  16. 16

    Format integer number with . as thousand separator and no decimals

  17. 17

    Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA?

  18. 18

    comma separator using implode not working

  19. 19

    If decimal value, convert to two decimals AND dot separated value to comma separated

  20. 20

    SQL Server DTS/SSIS decimals dot instead of comma

  21. 21

    Use comma instead of dot for decimals when saving as text

  22. 22

    SQL Server DTS/SSIS decimals dot instead of comma

  23. 23

    How to change position of data columns using regex in a CSV file using comma as separator?

  24. 24

    Convert number to string without any formating and using "." as floating point separator

  25. 25

    Regex everything but number with two decimals

  26. 26

    Google form regex for numbers with comma as decimal separator

  27. 27

    Python: Detect number separator symbols and parse into a float without locale

  28. 28

    Jaxb conversion of number with comma as a decimal separator

  29. 29

    r ggplot with zeroes and no comma as the big number separator

HotTag

Archive