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.14Matches
3.14
Input
-0.001Matches
-0.001
Input
42Matches
42