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
(?:\+?(1)|(1?))[\s_.-]?\(?(\d{3})\)?[\s_.-]?(\d{3})[\s_.-]?(\d{4})
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)
(?:\+?(1)|(1?))([\s_.-]?)\(?(\d{3})\)?\3(\d{3})\3(\d{4})
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
((\d{5})(?:-(\d{4}))?)
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)
(A[AELKSZRP]|C[AOT]|D[EC]|F[ML]|G[AU]|HI|I[DLNA]|K[SY]|LA|M[EHDAINSOTP]|N[EVHJMYCD]|O[HKR]|P[WAR]|RI|S[CD]|T[NX]|UT|V[TIA]|W[AVIY])
Matches US state codes, consisting of specific 2 character upper-case letters. Group captures the entire 2 character code.

IP Addresses

Basic validation
(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
This will validate IP addresses of the form 0-255.0-255.0-255.0-255. Groups capture each octet.

Email

Very basic validation
([\w\d._%+]+)@([\w\d.]+\.[a-zA-Z]{2,4})
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 .@..com.
Basic validation
([\w\d_%+]+(?:\.[\w\d_%+]+)*)@((?:[\w\d_]+\.)*[\w\d]+\.[A-Za-z]{2,63})
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
(?:0x)?((?:[a-fA-F\d]{2})+)
Finds strings with or without 0x prefix. Ensures full bytes (double hex characters).
Hexadecimal color validator
(?:#?([a-fA-F\d]{6}|[a-fA-F\d]{3}))
Finds strings with or without # prefix. Ensures 3 or 6 nibbles (#FFF or #FFFFFF).

HTML

Fairly basic tag parsing
<(\w+)(?:\s(\w+)="?([^"]+)")*>([^<]*)</\1>
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 (\)
\0n
Octal character value 0n
\xn
Hexidecimal character value 0xn
\x{n...n}
Hexidecimal character value 0xn...n
\t
Tab character
\v
Vertical tab character
\n
Newline character
\r
Carriage-return character
\f
Form-feed character
\a
Alert (bell) character
\e
Escape character
\cX
Control character corresponding to X

Regex Predefined Character Classes

Character Class Matches
.
Any character (except \n depending on engine)
\d
Any digit character (0-9)
\D
Any non-digit character (not 0-9)
\s
Any whitespace character
\S
Any non-whitespace character
\w
Any word character
\W
Any non-word character
[xgn]
x, g, or n
[^xgn]
Not x, g, or n
[a-zA-Z]
All uppercase and lowercase letters
[xgn[aei]]
x, g, n, a, e, or i (union)
[n-z&&[xgn]]
x or n (intersection)
[a-z&&[^bc]]
All lowercase letters but not b or c (subtraction)
[a-z&&[^g-i]]
All lowercase letters but not g, h, or i (subtraction)

Regex POSIX Character Classes

Java Pattern POSIX Pattern Matches
\p{Lower}
[:lower:]
All lowercase letters [a-z]
\p{Upper}
[:upper:]
All uppercase letters [A-Z]
\p{ASCII}
All ASCII characters
\p{Alpha}
[:alpha:]
All uppercase and lowercase letters [A-Za-z]
\p{Digit}
[:digit:]
All digits [0-9]
\p{Alnum}
[:alnum:]
All letters and digits [A-Za-z0-9]
\p{Punct}
[:punct:]
All punctuation [!"#$%&'()*+,\-./:;<=>?@\[\]^_`{|}~\\]
\p{Graph}
[:graph:]
All letters, digits, and punctuation
\p{Print}
[:print:]
All graph characters and space
\p{Blank}
[:blank:]
Space or tab [\t ]
\p{Cntrl}
[:cntrl:]
All control characters
\p{XDigit}
[:xdigit:]
All hexidecimal characters [A-Fa-f0-9]
\p{Space}
[:space:]
All whitespace characters [ \t\n\v\f\r]

Regex Quantifiers

Symbol Greedy Usage Reluctant Usage Matches
?
X?
X??
Proceeding construct 0 or 1 time
*
X*
X*?
Proceeding construct 0 or more times
+
X+
X+?
Proceeding construct 1 or more times
{n}
X{n}
X{n}?
Proceeding construct n times
{n,}
X{n,}
X{n,}?
Proceeding construct n or more times
{n,m}
X{n,m}
X{n,m}?
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
|
A|B
Matches A or B

Regex Groups & Back-references

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