Match a percentage
Find a percentage value, decimals included, wherever it appears in a report, review, or spreadsheet export.
Last updated
Matches
- Growth was 12.5% this year
- Discount: 20%
Doesn’t match
- Full percent sign %
- Discount was 20 percent off
Why it’s written this way
\d+ requires at least one digit before the sign, and the optional (?:\.\d+)? group allows a decimal point followed by more digits, so a whole number like 20% and a fractional one like 12.5% both match under the same rule.
The percent sign is required immediately after the digits, with no gap allowed, which keeps the pattern from wandering into unrelated numbers that merely sit near a % elsewhere in the text.
Edge cases to know
- →It won't catch percentages spelled out in words, such as '20 percent' — only the numeral-plus-symbol form counts.
- →A percentage written with a space before the sign, like '20 %', is missed for the same reason a bare '%' with nothing in front of it is: the digits and the symbol must be glued together.
Part by part
- \d+
- One or more digits for the whole-number part. Unbounded, so 100 and 1000 both match.
- (?:\.\d+)?
- An optional decimal portion, which is what lets 12.5% match alongside 12%.
- %
- The literal percent sign. It has no special meaning in regex, so it needs no escaping.
Common mistakes
\d+%Without the optional decimal group, 12.5% matches only as 5%, silently reporting a completely different figure — a quiet wrong answer rather than a visible failure.
\d+(?:\.\d+)?%\d+(?:\.\d+)?\s*%Allowing whitespace is legitimate for prose written that way, but it also matches across a line break, pulling a number from one line and a percent sign from the next.
[\d.]+%A flat class accepts multiple dots, so "1.2.3%" matches. Structure, not alphabet, is what makes a decimal number well-formed.
Questions
Does it match negative percentages?
No, the minus sign is not included. Prepend -? if your data contains declines written that way — though many documents use a leading word or parentheses instead, which no pattern will catch.
Does it match percentage point differences?
It matches the number and sign only. The distinction between a percentage and a percentage point is semantic, and confusing the two is a common reporting error that no pattern can detect for you.
Will it match inside a word?
It can. In an identifier like abc50% the digits and sign match. Add a word boundary before the digits if you are scanning text where numbers appear inside identifiers.
How do I convert the match to a fraction?
Strip the sign and divide by 100, and keep the result as a decimal rather than a float where precision matters. A percentage in a financial context should be handled with the same care as a currency amount.
Does it match a percentage written as a fraction, like 1/4?
No, and nor should it — that is a different notation with no percent sign at all. If your source mixes the two, match them separately and convert, because a pattern that tried to cover both would match every date and every ratio in the document as well.