Python (re)

Float / Scientific Number in PY

Match floating-point and scientific-notation numbers including `1.5`, `.25`, `1e10`, `-3.14E-2`.

Try it in the PY tester →

Pattern

regexPY
[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?")
input_text = "h = 6.626e-34, c = 3e8, alpha = .007"
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

[-+]? matches an optional sign. (?:\d+\.?\d*|\.\d+) matches the mantissa as either digits-with-optional-decimal or decimal-with-trailing-digits. (?:[eE][-+]?\d+)? matches the optional exponent. Covers most real-number literals you'll encounter in source or data.

Examples

Input

h = 6.626e-34, c = 3e8, alpha = .007

Matches

  • 6.626e-34
  • 3e8
  • .007

Input

Range -1.5 to +2.5

Matches

  • -1.5
  • +2.5

Input

no numbers here

No match

Same pattern, other engines

← Back to Float / Scientific Number overview (all engines)