python - Regex match numbers with commas and points -


i match integers , floats using re module. if types 1 of following input types, should validate number:

- 1000 - 1.000 - 1,000 - ($1,000.98) - -1.000 - 1.0 - $1,0000 

right using following:

"^[-+]?[0-9]+$" 

any appreciated

for given input regex should work:

^(?:[+-]|\()?\$?\d+(?:,\d+)*(?:\.\d+)?\)?$ 

regex demo

breakup:

  • ^ - start
  • (?: - start non-capturing group
    • [+-] - match + or -
    • | - or
    • \( - match ( )? - end non-capturing group (optional)
  • \$? - match $ (optional)
  • \d+ - match 1 or more digits
  • (?: - start non-capturing group
    • , - match comma
    • \d+ - match 1 or more digits
  • )* - end non-capturing group (zero or more occurrence)
  • (?: - start non-capturing group
    • \. - match dot
    • \d+ - match 1 or more digits
  • )? - end non-capturing group (optional)
  • \)? - match literal ) (optional) in end
  • $ - end

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -