DD
DevDash

Regex for Percentage Value

Regex Pattern

^-?\d+(?:\.\d+)?%$

Numeric percentage with % sign

Quick Answer

The regex pattern for percentage value is `^-?\d+(?:\.\d+)?%$`. Numeric percentage with % sign. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
100%✓ Matches
50.5%✓ Matches
-3.14%✓ Matches
0%✓ Matches
100✗ No match
50%50✗ No match
% only✗ No match

Code Examples

javascript

const regex = /^-?\d+(?:\.\d+)?%$/;
const isValid = regex.test(value);

python

import re
pattern = r'^-?\d+(?:\.\d+)?%$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^-?\d+(?:\.\d+)?%$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^-?\d+(?:\.\d+)?%$/', $value)) {
    echo "valid";
}

java

String pattern = "^-?\\d+(?:\\.\\d+)?%$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.