Limited Offer
60% OFF on all plans!

Regex in Java

Write regular expressions using the java.util.regex package — Pattern, Matcher, find, matches, groups, and common real-world patterns.

Introduction to Regular Expressions

1. What is a Regular Expression?

A regular expression (regex) is a pattern used to match, search, or validate text. Java provides regex support through `java.util.regex.Pattern` and `java.util.regex.Matcher`.

Creating Regular Expressions

1. Pattern.compile (Recommended)

`Pattern.compile(regex)` compiles the pattern once and reuses it. Always prefer this for performance — especially in loops.

2. String.matches (Shorthand)

`String.matches(regex)` is a shorthand — but it compiles the pattern on every call and requires a full match of the entire string.

Regex Flags

1. Common Flags

Pass flags as the second argument to `Pattern.compile()`. Common ones: `CASE_INSENSITIVE`, `MULTILINE`, `DOTALL`.

Regex Methods

1. matches() — Full String Match

`matcher.matches()` returns true only if the entire string matches the pattern. Use `find()` for partial matching.

2. find() — Search / Find All

`matcher.find()` scans for the next match. Call it in a loop to find all occurrences. `matcher.group()` returns the matched text.

3. replaceAll() — Replace Matches

`String.replaceAll(regex, replacement)` replaces all matches. `replaceFirst()` replaces only the first match.

4. split() — Split by Pattern

`String.split(regex)` splits the string wherever the pattern matches.

Character Classes

1. Common Character Classes

Java uses the same character class syntax as most regex flavors. Double-escape backslashes in Java strings: `\d` in regex → `"\\d"` in Java string.

Quantifiers

1. Common Quantifiers

`*` zero or more, `+` one or more, `?` zero or one, `{n}` exactly n, `{n,m}` between n and m.

Anchors

1. ^ and $ Anchors

`^` matches start of string, `$` matches end. Use both to enforce an exact full match.

Groups & Alternation

1. Capturing Groups

Wrap part of a pattern in `()` to capture it. Access with `matcher.group(1)`, `matcher.group(2)`, etc.

2. Alternation (OR)

`|` works like OR — matches either the left or right pattern.

Regex for Validation

1. Email Validation

A basic regex to check if a string looks like an email address.

2. Numeric String Validation

Validate integers, decimals, and phone numbers using regex.