Java Regex Testing Tool

Java Regex Tester

Please enter a Java Regular Expression pattern (no need to surround with /'s), some text to apply the Regex pattern to, and an optional replacement string. Choose which operations you'd like to have performed, and any number of flags to apply, then submit!

If you'd like to see an example, jump to the common patterns section and use the 'Load in Testing Tool' button.

*use $n to print groups, where n is the group number ($1, $2, etc) - use $0 for the entire match*
Operations to Perform
  • java.util.regex.Matcher javadoc
  • java.lang.String javadoc
  • java.util.regex.Pattern javadoc
Flags (mouse-over for description)

Regular Expression Patterns for Common Tasks

Phone Numbers

US validator
Matches US 10 and 11 digit numbers with an optional + prefix when followed by a 1. Groups capture the actual digits. Allows for differing separators.
US validator (more strict)
Matches US 10 and 11 digit numbers with an optional + prefix when followed by a 1. Groups capture the actual digits. Forces the same separator character between each digit group.

US Addresses

US ZIP code validator
Matches US 5 and 9 digit ZIP codes. Groups capture the entire 9 digits and hyphen, as well as the separate number groups.
US state code validator (2 char)
Matches US state codes, consisting of specific 2 character upper-case letters. Group captures the entire 2 character code.

IP Addresses

Basic validation
This will validate IP addresses of the form 0-255.0-255.0-255.0-255. Groups capture each octet.


Very basic validation
This is quite simple and will validate basic email addresses. It's not great though and has some obvious issues. For instance, it would match the string
Basic validation
This works fairly well for the vast majority of email addresses. It's not perfect, but will fit the vast majority of email address out there without being unreasonably large and complicated. To get a better idea of why one might make a tradeoff, check out the full RFC compliant email validation pattern below!
Full validation
Click here to view pattern
This pattern is so big I can't even fit it here. It does properly validate all email (as far as I know), at the cost of being unwieldy. It may be more efficient to parse using methods other than Regex.

Number Bases

Hexadecimal byte string validator
Finds strings with or without 0x prefix. Ensures full bytes (double hex characters).
Hexadecimal color validator
Finds strings with or without # prefix. Ensures 3 or 6 nibbles (#FFF or #FFFFFF).


Fairly basic tag parsing
Matches paired and un-nested HTML tags. Groups capture the tag name, last attribute name and value, and the content of the tag body.

Java Regular Expression Reference

Standard Regex Meta-Characters

Character Matches
The backslash character (\)
Octal character value 0n
Hexidecimal character value 0xn
Hexidecimal character value 0xn...n
Tab character
Vertical tab character
Newline character
Carriage-return character
Form-feed character
Alert (bell) character
Escape character
Control character corresponding to X

Regex Predefined Character Classes

Character Class Matches
Any character (except \n depending on engine)
Any digit character (0-9)
Any non-digit character (not 0-9)
Any whitespace character
Any non-whitespace character
Any word character
Any non-word character
x, g, or n
Not x, g, or n
All uppercase and lowercase letters
x, g, n, a, e, or i (union)
x or n (intersection)
All lowercase letters but not b or c (subtraction)
All lowercase letters but not g, h, or i (subtraction)

Regex POSIX Character Classes

Java Pattern POSIX Pattern Matches
All lowercase letters [a-z]
All uppercase letters [A-Z]
All ASCII characters
All uppercase and lowercase letters [A-Za-z]
All digits [0-9]
All letters and digits [A-Za-z0-9]
All punctuation [!"#$%&'()*+,\-./:;<=>?@\[\]^_`{|}~\\]
All letters, digits, and punctuation
All graph characters and space
Space or tab [\t ]
All control characters
All hexidecimal characters [A-Fa-f0-9]
All whitespace characters [ \t\n\v\f\r]

Regex Quantifiers

Symbol Greedy Usage Reluctant Usage Matches
Proceeding construct 0 or 1 time
Proceeding construct 0 or more times
Proceeding construct 1 or more times
Proceeding construct n times
Proceeding construct n or more times
Proceeding construct at least n but no more than m times

Regex Escaping & Quoting

Pattern Usage Meaning
When escaping does not produce a meta-character matches the following character, in the example, a dot
\Q and \E
\Qliteral text.\E
Uses the string literal between the start, \Q, and end, \E, ignoring special meanings of characters

Regex Logical Operators

Operator Usage Meaning
Matches A or B

Regex Groups & Back-references

Pattern Usage Meaning
Creates a capturing group, matching and capturing A
(?<name>To Match)
Creates a named capturing group with the given name
(?:To Match)+
Creates a non-capturing group
Matches the proceeding captured group n (ordered left to right)
Matches the proceeding captured group with the given name