Python (re)

Decimal Number in PY

Matches decimal numbers, including integers and negatives.

Try it in the PY tester →

Pattern

regexPY
^-?\d+(\.\d+)?$

Python (re) code

pyPython
import re

pattern = re.compile(r"^-?\d+(\.\d+)?$")
input_text = "3.14"
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

`^-?\d+` matches optional sign and integer part. `(\.\d+)?` optionally matches a decimal point and fractional digits.

Examples

Input

3.14

Matches

  • 3.14

Input

-0.001

Matches

  • -0.001

Input

42

Matches

  • 42

Same pattern, other engines

← Back to Decimal Number overview (all engines)