Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for percentage
Regex pattern · validation

Regex for percentage

Regex for percentage: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^(?:100(?:\.0+)?|\d{1,2}(?:\.\d+)?)%$/ as a percentage regex. It matches "50%" and "99.5%", and rejects "101%" and "50". Make the % optional with %?$ if users may omit it.

Last updated · Maintained by FreeAIToolz · How we test

/^(?:100(?:\.0+)?|\d{1,2}(?:\.\d+)?)%$/
  • 50%matches
  • 99.5%matches
  • 100%matches
  • 0%matches
  • 101%no match
  • 50no match
  • -5%no match
  • 100.5%no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
100(?:\.0+)?exactly 100 (optionally 100.0)
|\d{1,2}(?:\.\d+)?or 0–99.99…
%$a percent sign at the end

Examples

InputResult
50%✓ Matches
99.5%✓ Matches
100%✓ Matches
0%✓ Matches
101%✗ No match
50✗ No match
-5%✗ No match
100.5%✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Make the % optional with %?$ if users may omit it.

When to use this pattern

Discount fields, survey inputs and progress values.

Use it in your language

JavaScript

const re = /^(?:100(?:\.0+)?|\d{1,2}(?:\.\d+)?)%$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^(?:100(?:\.0+)?|\d{1,2}(?:\.\d+)?)%$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^(?:100(?:\\.0+)?|\\d{1,2}(?:\\.\\d+)?)%$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^(?:100(?:\\.0+)?|\\d{1,2}(?:\\.\\d+)?)%$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^(?:100(?:\.0+)?|\d{1,2}(?:\.\d+)?)%$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for percentage?
/^(?:100(?:\.0+)?|\d{1,2}(?:\.\d+)?)%$/. Make the % optional with %?$ if users may omit it.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abc50%xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.