Skip to content
Tutorials Center
My learning

Topics

Lesson 107 of 201 ยท Working with Data

Regular Expressions

Learn Java regular expressions - regex syntax, Pattern and Matcher, matches vs find, capturing and named groups, flags, replaceAll with functions, split and performance tips.

A regular expression (regex) is a small pattern language for describing text: "one or more digits," "a word that starts with a capital letter," "something that looks like a date." With one pattern you can validate input, find every match in a document, pull out pieces such as the year and month, or rewrite text in a single call. Almost every language supports regexes, and Java's flavor in java.util.regex is close to the ones in JavaScript, Python and Perl.

This lesson teaches the syntax you'll use 95% of the time, the Pattern and Matcher classes, the difference between matches and find, capturing and named groups, flags, replacing with strings and with functions, splitting, and the performance and readability rules that keep regexes from becoming a liability. You'll want String Methods fresh in mind, since String has several regex-powered methods too.

Regex syntax in one page

A pattern is made of literal characters (which match themselves) and special constructs:

Construct Matches
. Any character except a line break
\d, \w, \s A digit, a word character ([a-zA-Z0-9_]), whitespace
\D, \W, \S The opposites: non-digit, non-word, non-whitespace
[abc], [a-z], [^0-9] One character from a set, a range, or not in a set
^, $ Start and end of the input (or of a line in MULTILINE mode)
\b A word boundary
*, +, ? 0 or more, 1 or more, 0 or 1 of the previous item
, {2,5}, {2,} Exactly 3, 2 to 5, at least 2
a|b a or b
(...) A capturing group
(?<name>...) A named capturing group
(?:...) A non-capturing group (grouping only)
\., \(, \\ A literal ., ( or backslash (escape special characters)

The double backslash

In Java source code, a backslash inside a string literal starts an escape sequence (\n, \t), so to put the regex \d into a string you must write "\\d". A regex for a literal backslash, \\, becomes "\\\\" in Java. Text blocks don't change this. When a pattern looks wrong, print it: you'll see exactly what the regex engine receives.

Character classes in depth

Square brackets can do more than list characters. Java supports negation ([^xyz] is any character except x, y, z), union ([a-c[x-z]] is a to c or x to z), intersection ([a-z&&[def]] is d, e or f) and subtraction ([a-z&&[^m-p]] is a lowercase letter that is not m to p). Predefined POSIX-style classes such as \p{Alpha}, \p{Upper}, \p{Digit} and \p{Punct} match ASCII categories, while Unicode properties such as \p{L} (any letter), \p{Lu} (uppercase letter) and \p{IsDigit} work for all scripts.

Java Negation, union, intersection and properties

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Pattern, Matcher and the three ways to match

The regex API has two main classes:

  • Pattern: a compiled regex. Create it with Pattern.compile(regex); it's immutable and thread-safe.
  • Matcher: a stateful engine that applies a pattern to one input. Get it with pattern.matcher(text).

A matcher offers three ways to match:

  • matches(): does the entire input match the pattern?
  • lookingAt(): does the beginning of the input match?
  • find(): is there a match anywhere? Call it repeatedly to find the next match each time.
Java matches vs lookingAt vs find

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

String.matches is a convenient shortcut for whole-string validation, but beginners often expect it to behave like find. It doesn't: "ID-77".matches("\\d+") is false because the whole input must match. To search inside text, use find().

Since Java 9, matcher.results() returns a stream of all matches as MatchResult objects, which is often neater than a while loop:

Java
List<String> numbers = digits.matcher("12, 7 and 104").results()
    .map(MatchResult::group)
    .toList();   // [12, 7, 104]

Capturing groups

Parentheses do two jobs: they group parts of a pattern (so (ab)+ repeats ab), and they capture the text each group matched. Groups are numbered from 1 by their opening parenthesis, left to right; group(0) or group() is the whole match.

Named groups, (?<year>\d{4}), are far more readable than numbers and survive changes to the pattern. Read them with m.group("year").

Java Numbered and named groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

(?:...) groups without capturing, so the cents are group 2, not 3. A group that didn't participate in the match returns null, which is why "250" prints cents=null. Calling group() before a successful matches() or find() throws IllegalStateException.

Quantifiers: greedy, lazy and possessive

Quantifiers are greedy by default: they match as much as possible and give characters back only if the rest of the pattern needs them. Adding ? makes them lazy (as little as possible). Adding + makes them possessive: they never give characters back, which can prevent slow backtracking.

Java Greedy vs lazy quantifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The greedy <.+> runs from the first < to the very last >. The lazy version stops at the first >. The negated class [^>]+ is usually the best choice: it says exactly what's allowed and needs no backtracking.

Flags

Flags change how a pattern is interpreted. Pass them to Pattern.compile(regex, flags), combined with |, or embed them at the start of the regex:

Flag Inline Effect
Pattern.CASE_INSENSITIVE (?i) Ignore case (ASCII by default)
Pattern.UNICODE_CASE (?u) With (?i), ignore case for all Unicode letters
Pattern.MULTILINE (?m) ^ and $ match at every line start/end
Pattern.DOTALL (?s) . also matches line breaks
Pattern.COMMENTS (?x) Whitespace is ignored and # starts a comment
Pattern.UNICODE_CHARACTER_CLASS (?U) \w, \d, \b follow Unicode, not just ASCII
Java Case-insensitive, multiline and commented patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Comments mode ((?x)) inside a text block is the best way to keep a complex regex readable. Remember that in this mode a literal space must be written as \s or [ ].

Replacing text

String.replaceAll(regex, replacement), replaceFirst and Matcher.replaceAll substitute every match. In the replacement string, $1 or ${name} inserts a captured group. Since that makes $ and \ special, use Matcher.quoteReplacement(text) for replacement text that comes from users or data.

Since Java 9, Matcher.replaceAll(Function<MatchResult, String>) computes each replacement with code, which handles cases a fixed string can't, like converting units or looking values up in a map:

Java replaceAll with groups and with a function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The template example needs quoteReplacement because the value $42.00 contains a $; without it, Java would try to read $4 as group 4 and throw an exception. The same applies to \.

Related: Pattern.quote(text) turns any text into a literal pattern, so "1+1".split(Pattern.quote("+")) splits on a real plus sign. If you only need literal replacement, String.replace(target, replacement) uses no regex at all and is both simpler and faster.

Splitting text

String.split(regex) and Pattern.split cut text at every match. Two rules surprise people:

  • Trailing empty strings are removed by default. Pass a negative limit, split(",", -1), to keep them, which matters for CSV-like data with empty last fields.
  • Special characters need escaping: split(".") splits on every character (the dot matches anything) and returns an empty array. Use split("\\.").
Java split with limits, escapes and splitAsStream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Advanced Matcher API

A Matcher is a small state machine, and a few of its less-known methods solve problems that find() and group() alone cannot.

  • region(start, end) limits the search to part of the input without copying a substring, so positions from start() and end() still refer to the original text. By default the region has anchoring bounds (^ and $ match at the region edges) and transparent bounds off (lookahead and lookbehind cannot see outside the region). Change either with useAnchoringBounds(false) and useTransparentBounds(true).
  • usePattern(newPattern) swaps the pattern but keeps the position, which is handy for tokenizers that try a different pattern for each kind of token. Group information from the previous match is lost.
  • toMatchResult() returns an immutable snapshot of the current match. A live Matcher changes with every find(), so save a MatchResult whenever you need to keep a match after moving on. That is exactly what results() hands you.
Java region, usePattern and toMatchResult

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The region 5..17 covers id=42 id=900 only, so the first 7 is never seen. The second block shows that ^ matched at the region start until anchoring bounds were switched off.

hitEnd, requireEnd and building output

hitEnd() tells you whether the last match attempt ran into the end of the input. If it is true after a failed lookingAt(), more text might still complete a match, which is how incremental parsers decide between "invalid" and "need more data". requireEnd() is the mirror image after a successful match: true means that more input could have turned the success into a failure (a $ or \b at the end made the match depend on the input stopping there).

For custom replacement logic, the classic loop is appendReplacement plus appendTail: for every match, appendReplacement(sb, text) copies the unmatched text since the previous match and then your replacement; appendTail(sb) copies whatever is left. Since Java 9 they accept a StringBuilder (older code needed StringBuffer). The replacement string is parsed for $1 and \, so wrap computed text in Matcher.quoteReplacement. For simple cases replaceAll(Function) (above) is shorter; use the loop when you also need side effects such as counting.

Java hitEnd, requireEnd, appendReplacement and results

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

results() streams MatchResult snapshots, so it composes with groupingBy, map and the rest of the Stream API.

Performance and good habits

  • Precompile patterns you reuse. String.matches, replaceAll and split compile the regex on every call (except for a fast path with single-character split separators). In a loop or a hot method, store a static final Pattern and reuse it. Pattern is thread-safe; Matcher is not, so create one matcher per use.
  • Use asMatchPredicate() or asPredicate() to plug a pattern into streams: names.stream().filter(VALID.asMatchPredicate()) (whole string) or asPredicate() (find anywhere).
  • Beware catastrophic backtracking. Nested quantifiers like (a+)+$ or (\w+\s?)*$ can take exponential time on inputs that almost match, freezing a server on a crafted string (a "ReDoS" attack). Prefer specific character classes, avoid nested repetition, use possessive quantifiers or atomic groups (?>...), and limit input length.
  • Don't parse structured formats with regex. HTML, JSON, XML and CSV with quotes have nesting and escaping rules that regexes handle poorly. Use a real parser (see Advanced Java: JSON with Jackson).
  • Keep validation realistic. A fully RFC-compliant email regex is monstrous. A simple check like ^[^@\s]+@[^@\s]+\.[^@\s]+$ catches typos; the only real proof is sending a confirmation email.

Here are a few practical patterns, each a starting point rather than a law:

Java
static final Pattern DIGITS_ONLY = Pattern.compile("\\d+");
static final Pattern EMAILISH = Pattern.compile("[^@\\s]+@[^@\\s]+\\.[^@\\s]+");
static final Pattern ISO_DATE = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");      // then parse with LocalDate
static final Pattern HEX_COLOR = Pattern.compile("#(?:[0-9a-fA-F]{3}){1,2}");
static final Pattern USERNAME = Pattern.compile("[a-z][a-z0-9_]{2,15}");

A regex can say a date looks right (2025-02-31 matches ISO_DATE), but only LocalDate.parse knows February has no 31st. Combine a regex for shape with real parsing for meaning (Dates and Times).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does "Order 66".matches("\\d+") return?

    String.matches checks the entire input. To look for digits anywhere, use a Matcher with find().
  2. 2. In (\d+)(?:-(\d+))?(x), which group number captures the x?

    Only capturing groups are numbered: the first (\d+) is group 1, the (\d+) inside the non-capturing group is group 2, and (x) is group 3.
  3. 3. What does "a.b.c".split(".").length return?

    The dot matches any character, so every character is a separator. All resulting strings are empty and trailing empty strings are removed, leaving an empty array.
  4. 4. Applied to <p>Hi</p>, what does the lazy pattern <.+?> find first?

    The lazy quantifier +? matches as few characters as possible, so the match ends at the first closing bracket.
  5. 5. Why should a pattern used inside a loop be stored as a static final Pattern?

    Pattern.compile parses the regex into an internal structure. Compiling once and reusing the immutable, thread-safe Pattern avoids repeating that work.

Summary

  • A regex describes text with classes (\d, [a-z]), anchors (^, $, \b), quantifiers (*, +, ?, {n,m}) and groups. In Java strings, double every backslash: "\\d".
  • Pattern.compile builds an immutable, thread-safe pattern; matcher.matches() checks the whole input, lookingAt() the start, and find() searches anywhere; results() streams all matches.
  • Groups capture text: group(1), or named groups (?<name>...) with group("name"); (?:...) groups without capturing.
  • Quantifiers are greedy by default; +? is lazy and ++ possessive. A negated class like [^>]+ is often the clearest.
  • Flags such as CASE_INSENSITIVE, MULTILINE and COMMENTS (or (?i), (?m), (?x)) change matching.
  • replaceAll supports $1/${name} and, since Java 9, a function per match; use quoteReplacement for data containing $ or \.
  • split drops trailing empty strings unless you pass a negative limit, and special characters need escaping.
  • Precompile reused patterns, avoid nested quantifiers that backtrack catastrophically, and use real parsers for structured formats.

Next: BigInteger and BigDecimal, for integers of any size and exact decimal arithmetic.