Advertisement

Pattern library / Validation

Float Regex

Checking whether a string is a decimal number — regex match float, regex match decimal number — usually gets answered with \d+, which only proves that digits are present: abc123 passes, 3.14 does not, and 1,234.56 is quietly rejected. The pattern below validates the whole value instead: ^[+-]?(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?$ — an optional sign, then a mantissa that must contain at least one digit (digits with a fractional part, or a fraction that starts with a dot), then an optional exponent. Because every mantissa branch requires digits, the empty string fails here rather than passing the way it does in the usual ^\d*\.?\d*$ copy-paste. Literals, classes, groups and quantifiers only: no lookahead, no backreference, so it runs unchanged in JavaScript, Python and BigQuery (RE2).

The pattern
^[+-]?(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
3.14Matchdigits, one dot, digits — the plain mantissa branch \d+(\.\d+)?Try →
-0.5Matchthe optional sign [+-]? takes the minusTry →
+2Matchan explicit plus is allowed and the fractional part is optionalTry →
.5Matchthe second mantissa branch: a fraction that starts with a dotTry →
5.No matcha dot with no digits after it is not a number — see pitfall 1Try →
1e-3Matchexponent branch: e, optional sign, digitsTry →
1E+10Matchuppercase E with a plus exponent — same branchTry →
No matchthe empty string: every mantissa branch requires digits, so nothing passesTry →
1,234.56No matchthe thousands-separator comma is not part of the pattern — see pitfall 2Try →
0x1FNo matchhex is not a decimal float: 0 matches, x stops itTry →
3.14.15No matchone decimal point only — the second dot has no parseTry →
3.14No matchsurrounding whitespace fails the anchors — trim firstTry →

Where to use it

JavaScript (validate a form value, then find decimals in text)

const FLOAT = /^[+-]?(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?$/;

FLOAT.test('3.14');  // true
FLOAT.test('1e-3');  // true
FLOAT.test('5.');     // false - the trailing dot needs a digit
FLOAT.test('');       // false - the mantissa requires digits

// find decimals in text: pull candidates, then keep the valid ones
const cands = 'pi is 3.14 and e is 2.71828'.match(/[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?/g) || [];
cands.filter(s => FLOAT.test(s)); // ['3.14', '2.71828']

Python (fullmatch + extract candidates)

import re
PAT = r'^[+-]?(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?$'
re.fullmatch(PAT, '3.14')  # re.Match - valid float
re.fullmatch(PAT, '5.')    # None - trailing dot rejected
re.fullmatch(PAT, '')      # None - digits are required

# pull candidates out of a text, keep the valid ones
cands = re.findall(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?', 'pi is 3.14 and e is 2.71828')
[c for c in cands if re.fullmatch(PAT, c)]   # ['3.14', '2.71828']

BigQuery SQL (RE2 - flag rows whose value is a decimal number)

SELECT sku,
  REGEXP_CONTAINS(sku, r'^[+-]?(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?$') AS is_float
FROM `project.dataset.metrics`
-- '3.14' -> true | '5.' -> false | '1,234.56' -> false | '' -> false

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

How do I match a decimal number with regex?

Use ^[+-]?(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?$ — optional sign, a mantissa that must contain digits (3.14 or .5), optional exponent (1e-3). The anchors make it a validator; drop them and use the candidate recipe in the last pitfall to find numbers inside text.

Why does my float regex accept an empty string or 5.?

Because the usual shortcut ^\d*\.?\d*$ makes every part optional: nothing matches, and so does a trailing dot. This pattern requires digits in the mantissa, so the empty input is a no-match (verified above), and 5. still fails because a dot with no digits after it is not a number. If your data really contains trailing dots, use the variant from pitfall 1.

Does this pattern work in GA4, BigQuery and Python?

Yes — only literals, classes, groups and quantifiers, so nothing RE2 refuses (see RE2 vs PCRE). Python: re.fullmatch(PAT, s). BigQuery: REGEXP_CONTAINS(x, r'^[+-]?(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?$'). GA4 filters are full-match, so the anchors are implied and the only letters involved are e/E in the exponent — more in the GA4 regex match types guide.

Extract Numbers from String — Regex + Examples · Regex Number in Range 1-100 — Match Values Between Limits · Percentage Regex — Match Percent Values — Test & Copy

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.