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).
How the pattern works
- Read it left to right: optional sign
[+-]?, mantissa(\d+(\.\d+)?|\.\d+), optional exponent([eE][+-]?\d+)?. The mantissa has two branches — digits with a fraction, or a fraction that starts with a dot — and both demand at least one digit. That single requirement is what makes.5pass while the empty input and5.fail. - Two branches beat one: the usual shortcut
^\d*\.?\d*$makes every part optional, so it accepts nothing at all as well as5., and you end up bolting a length check onto the result. Here the guard is built into the pattern — verified in the table, where the empty input is a no-match. - The exponent branch is optional, so plain integers like
42also match — normally what you want from a number validator. If you specifically need a decimal point, drop the alternation:^[+-]?\d+\.\d+([eE][+-]?\d+)?$. If you need to exclude the exponent instead, drop the last group:^[+-]?(\d+(\.\d+)?|\.\d+)$. - Nothing RE2 refuses: no lookahead, no lookbehind, no backreference — only literals, classes, groups and quantifiers, so the same pattern runs unchanged in JavaScript, Python and BigQuery. See RE2 vs PCRE; in GA4 every filter is full-match, so the anchors are already implied there.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
3.14 | Match | digits, one dot, digits — the plain mantissa branch \d+(\.\d+)? | Try → |
-0.5 | Match | the optional sign [+-]? takes the minus | Try → |
+2 | Match | an explicit plus is allowed and the fractional part is optional | Try → |
.5 | Match | the second mantissa branch: a fraction that starts with a dot | Try → |
5. | No match | a dot with no digits after it is not a number — see pitfall 1 | Try → |
1e-3 | Match | exponent branch: e, optional sign, digits | Try → |
1E+10 | Match | uppercase E with a plus exponent — same branch | Try → |
| No match | the empty string: every mantissa branch requires digits, so nothing passes | Try → |
1,234.56 | No match | the thousands-separator comma is not part of the pattern — see pitfall 2 | Try → |
0x1F | No match | hex is not a decimal float: 0 matches, x stops it | Try → |
3.14.15 | No match | one decimal point only — the second dot has no parse | Try → |
3.14 | No match | surrounding whitespace fails the anchors — trim first | Try → |
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
.5is accepted but5.is rejected. Most parsers (JSON,float()) accept both, so a trailing dot in your data fails validation here. Normalize first —s.replace(/\.$/, '')— or switch to the trailing-dot-tolerant variant^[+-]?\d+(\.\d*)?([eE][+-]?\d+)?$, which accepts5.on purpose.- Separators and specials are not numbers:
1,234.56,0x1F,NaNandInfinityare all no-matches (the first three verified above). Strip grouping characters before validating —s.replace(/,/g, '')— and give hex or the special float values their own patterns. - Anchors decide whether you validate or search. Unanchored,
\d+(?:\.\d+)?takes1.2and3out ofv1.2.3, and only1out of1e-3— version strings and exponents get chopped up. Recipe: pull candidates with[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?, then re-test each against the anchored pattern above. Surrounding whitespace also fails the anchors, so trim first.
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.
Related patterns
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.