Python (re)

US Currency (USD) in PY

Matches USD currency amounts with optional $ sign, thousands separators, and cents.

Try it in the PY tester →

Pattern

regexPY
^\$?\d{1,3}(,\d{3})*(\.\d{2})?$

Python (re) code

pyPython
import re

pattern = re.compile(r"^\$?\d{1,3}(,\d{3})*(\.\d{2})?$")
input_text = "$1,234.56"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

`^\$?` optional dollar sign. `\d{1,3}(,\d{3})*` matches the integer part with optional comma-separated thousands. `(\.\d{2})?` optional cents.

Examples

Input

$1,234.56

Matches

  • $1,234.56

Input

999

Matches

  • 999

Input

$1000000.00

No match

Same pattern, other engines

← Back to US Currency (USD) overview (all engines)