Percentage Regex

^-?[0-9]+(?:\.[0-9]+)?%$

Match percentage values with optional decimal places.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern ^-?[0-9]+(?:\.[0-9]+)?%$:

  • ^ - Start of string
  • $ - End of string
  • ? - Optional (zero or one)
  • + - One or more
  • [...] - Character class
  • (...) - Capturing group
  • (?:...) - Non-capturing group

Examples

Input Result Explanation
50% ✓ Match Whole percentage
99.99% ✓ Match With decimals
-5% ✓ Match Negative percentage

Code Examples

JavaScript

JavaScript
const pattern = /^-?[0-9]+(?:\.[0-9]+)?%$/;
const text = "50%";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'^-?[0-9]+(?:\.[0-9]+)?%$'
text = "50%"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/^-?[0-9]+(?:\.[0-9]+)?%$/';
$text = "50%";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "^-?[0-9]+(?:\\.[0-9]+)?%$";
String text = "50%";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Statistics, data visualization, finance

Frequently Asked Questions

What is the regex pattern for percentage regex?
The regex pattern is: ^-?[0-9]+(?:\.[0-9]+)?%$. Match percentage values with optional decimal places.
How do I use the percentage regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^-?[0-9]+(?:\\.[0-9]+)?%$'); or const regex = /^-?[0-9]+(?:\.[0-9]+)?%$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^-?[0-9]+(?:\.[0-9]+)?%$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for percentage regex?
Statistics, data visualization, finance

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: