A regular expression (regex) is a pattern that describes a set of strings, such as “five digits” or “a word that starts with cat“. In Java, the java.util.regex package compiles such a pattern into a Pattern object, and a Matcher object applies it to text. We use regex to validate input, find text, extract parts of it and replace it.
This page lists every Java regex tutorial on this site, grouped by topic. Each article starts with a quick-reference snippet, explains its patterns part by part, and has a runnable project with tests on GitHub, built with Java 25. Readers who are new to regex can start with the introduction to Java regular expressions.
The core Java regex calls fit in a few lines:
// 1. Whole input must match
"12345".matches("\\d{5}"); // true
// 2. Compile once, reuse
Pattern word = Pattern.compile("\\bcat\\b");
word.matcher("the cat sat").find(); // true
// 3. Extract every match
Pattern.compile("\\d+").matcher("a1 b22 c333")
.results().map(MatchResult::group).toList(); // [1, 22, 333]
// 4. Replace
"a b c".replaceAll("\\s+", " "); // "a b c"
1. Regex Basics in Java
These articles explain the building blocks of every pattern. Reading them in order gives a solid base for the validation articles.
- Java Regex Metacharacters: Complete List With Examples: every metacharacter, character class, quantifier and group with Java examples.
- [Regex Match Any Character: Dot, .*, DOTALL and [\s\S]](https://howtodoinjava.com/java/regex/match-any-set-of-characters/): the dot, .*, line breaks and character classes.
- Regex End of Line and Start of Line in Java (^, $): the anchors ^ and $, MULTILINE mode and line breaks.
- Regex Word Boundary in Java: Words That Start or End With: \b and finding words that start or end with a pattern.
- Regex to Match an Exact Word in Java (and Contains Word): matching an exact word or checking that text contains a word.
- Regex for Alphanumeric Characters in Java (With Examples): letters and digits only, in ASCII and in Unicode.
2. Validating Input With Regex
Each article gives a tested regex, explains it part by part, and says what the regex cannot check and which Java code covers the rest.
- Java Email Validation Using Regex: Simple to Strict: email addresses, from a simple check to a strict one.
- Java Password Validation Regex: Lookaheads Explained: password rules with lookaheads.
- Java Date Format Validation: Regex and LocalDate STRICT: date formats with regex and real dates with LocalDate.
- Regex for International Phone Numbers in Java (E.164): international phone numbers in E.164 format.
- Java Regex to Validate and Format US Phone Numbers: US and Canadian phone numbers and reformatting them.
- US ZIP Code Regex in Java: 5-Digit and ZIP+4 Validation: 5-digit and ZIP+4 codes.
- Canadian Postal Code Regex in Java (Canada Post Rules): Canadian postal codes such as A1A 1A1.
- UK Postcode Regex in Java: Validate All Six Formats: all six UK postcode formats.
- Credit Card Regex in Java: Brand Check and Luhn Validation: card number formats and the Luhn checksum.
- ISBN Regex in Java: Validate ISBN-10 and ISBN-13 Numbers: ISBN-10 and ISBN-13 with check digits.
- SSN Regex in Java: Validate Social Security Numbers: US Social Security numbers and the invalid ranges.
- Regex Min and Max Length in Java: {min,max} Explained: minimum and maximum input length with {min,max}.
- Regex to Limit the Number of Lines in Text (Java Examples): limiting the number of lines in text.
- Java Regex to Limit the Number of Words in Text Input: limiting the number of words in input.
3. Unicode, Symbols and Text Cleanup
These articles cover characters beyond plain ASCII, which Java regex handles through Unicode properties such as \p{L} and \p{Sc}.
- Remove Non-ASCII and Non-Printable Characters in Java: removing non-ASCII and non-printable characters.
- Java Regex to Match Greek Characters (Script vs Block): Greek letters, Unicode scripts and blocks.
- Java Regex to Match Any Currency Symbol with \p{Sc}: any currency symbol with \p{Sc}.
- Regex to Match the Trademark Symbol in Java (U+2122): the trademark symbol and similar marks.
- Regex for Misspelled Words: Match Every Spelling in Java: one pattern for every spelling of a word.
4. Conclusion
For a structured path, we read the basics group first, then pick the validation articles we need. The Unicode group helps when input comes from users in many languages.
5. References
The JavaDoc of the java.util.regex package is the full reference for the syntax and flags used in these tutorials.
Happy Learning !!