RegEx
In short: Regular expressions — a compact pattern language for checking text against a specific format or extracting parts from it. Available in Java via the Pattern and Matcher classes.
In more detail: String directly offers simple regex methods like matches(), replaceAll(), and split(). For repeated use of the same pattern, it’s worth compiling it once in advance with Pattern.compile(regex), instead of parsing the expression anew on every call.
boolean valid = email.matches("^[\\w.-]+@[\\w.-]+\\.\\w+$");In Depth
String email = "test@example.com";
boolean valid = email.matches("^[\\w.-]+@[\\w.-]+\\.\\w+$");
// Reusable pattern - worthwhile when the same pattern is needed often
Pattern pattern = Pattern.compile("\\d+"); // one or more digits
Matcher matcher = pattern.matcher("Order No. 12345 from customer 987");
while (matcher.find()) {
System.out.println(matcher.group()); // "12345", then "987"
}
// Common use cases
String cleaned = "Hello World".replaceAll("\\s+", " "); // multiple spaces -> one
String[] parts = "a,b,,c".split(","); // {"a", "b", "", "c"}Important escaping rule: in a Java string, a backslash itself has to be written as \\, because the backslash already has an escape meaning in normal Java strings (\n, \t, …) — a regex pattern like \d+ (one or more digits) therefore becomes "\\d+" in Java source code, which often confuses beginners when transferring regex examples from other languages. Pattern.compile() is especially worthwhile when the same pattern is used repeatedly (e.g. in a loop), because String.matches() internally recompiles the pattern on EVERY call — for one-off use, the direct String method route (matches, replaceAll, split) is simpler.
Groups — extracting parts of a match specifically
Parentheses in a pattern define “groups”, which can be individually extracted from a match — especially useful for extracting structured data from text:
Pattern date = Pattern.compile("(\\d{2})\\.(\\d{2})\\.(\\d{4})"); // DD.MM.YYYY
Matcher m = date.matcher("Appointment on 15.03.2026");
if (m.find()) {
System.out.println("Day: " + m.group(1)); // "15"
System.out.println("Month: " + m.group(2)); // "03"
System.out.println("Year: " + m.group(3)); // "2026"
}Since Java 7, groups can additionally be named ((?<year>\\d{4})), which makes accessing them via m.group("year") more readable than via numbered positions, which can easily get mixed up for more complex patterns.
Typical pitfalls
Regular expressions are powerful but easy to misjudge: . by default matches ANY character except a line break (not just a literal dot — use \\. for that), */+ are by default “greedy” (match as much as possible, not as little), which can lead to surprising results for nested structures like HTML tags. For structured formats like HTML/XML/JSON, the rule of thumb is therefore to use a dedicated parser instead of bending regex to fit — regex is well suited for simple, flat patterns (rough email validation, extracting digit sequences), but poorly suited for nested, recursive structures.
See also: Strings, Special Characters