Skip to main content
hank-builds.com

Decimal Number regex

Matches decimal numbers including negative.

/-?\d+\.\d+/g

What it matches

An optional minus sign, at least one digit, a decimal point, and at least one digit after it. Requiring digits on both sides is deliberate: it keeps the pattern from matching a bare full stop, and from swallowing the dot at the end of a sentence.

Railroad diagram

Read left to right. Every path through the diagram is a string the pattern accepts.

"-" \d \. \d

Drag to pan. Ctrl+scroll to zoom.

Breakdown

gFind all matches instead of stopping after the first
-?Literal "-", optional
\d+Digit (0-9), one or more
\.Literal "."
\d+Digit (0-9), one or more

Test cases

Matches

  • 3.14159
  • -12.5
  • 0.5

Does not match

  • 42
  • .5
  • 1.

Caveats

It requires a fractional part, so plain integers do not match. There is no support for exponents like 1.5e10, thousands separators, a leading plus, or the comma decimal mark used across most of Europe. It will also take the 1.2 out of a version string such as 1.2.3.

More numbers patterns

Test this against your own input in the regex tester, browse the full pattern library, or check the syntax cheat sheet.