Web
HTTP Status Code Regex
HTTP responses carry a three-digit status code whose first digit gives the class: 1 for informational, 2 for success, 3 for redirection, 4 for client errors and 5 for server errors. This pattern accepts any code from 100 to 599, which is the defined range, making it suitable for monitoring rule editors, API mock configuration and log filters. Adjust the first character class to target one class, for example [45] for errors. The honest limit is that it accepts unassigned codes such as 299, because it checks the range rather than the official registry.
Pattern (JavaScript)
/^[1-5][0-9]{2}$/Starts at the start of the line/string, then one of the characters 1-5, then a digit, exactly 2 times, then the end of the line/string.
Examples
Matches
- ✓ 200
- ✓ 404
- ✓ 503
Does not match
- ✗ 600
- ✗ 99
- ✗ 2000
Try it
0 matches (JavaScript engine)
200
404
503
600
99
2000Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[1-5][0-9]{2}$/g;
const text = "200";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[1-5][0-9]{2}$')
text = '200'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[1-5][0-9]{2}$/';
$text = '200';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[1-5][0-9]{2}$");
String text = "200";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[1-5][0-9]{2}$`)
text := "200"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[1-5][0-9]{2}$");
var text = "200";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[1-5][0-9]{2}$").unwrap();
let text = "200";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[1-5][0-9]{2}$/
text = '200'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I match only error codes?
- Change the first class to [45], giving ^[45][0-9]{2}$. That accepts 400 to 599, which covers both client and server errors.
- Can I find status codes inside access log lines?
- Replace the anchors with spaces or word boundaries around the code, since status codes appear between other fields. Be careful, because response sizes and times can also be three digits.
- Are codes above 599 ever used?
- The HTTP specification only defines classes 1 to 5. Some proprietary systems emit codes like 999, but they are non-standard, so the pattern rejects them.
Related patterns
- HTTP or HTTPS URL^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\s]*)?$
- Data URI^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$
- Domain Name^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$
- File Path with Extension^(?:[A-Za-z]:[\\/]|/)?(?:[\w .-]+[\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$