Regex series: 1. Syntax, the Java API and validation exercises · 2. Exercises: network configs, logs to CSV, lookbehind, duplicate lines · 3. The engine, backtracking and catastrophic backtracking · 4. ReDoS in Java: outages and defences
Backend work touches regex in many places (@Pattern, pulling fields out of logs, Nginx rules, grep); writing one that matches the example in front of you is easy, writing one that doesn’t match what it shouldn’t and that you can still read three months later is the hard part. This post covers the core syntax, Pattern/Matcher and the String methods that cause bugs, then solves three validation exercises (image file names, numbers, email); every pattern was run on JDK 21. Larger exercises are in part 2, the engine in part 3, ReDoS and its defences in part 4; String basics are in the arrays and strings post.
At a glance:
mindmap
root((Regex in Java))
Flavor: test on regex101
Syntax
Character classes, quantifiers
Anchors, lookaround, groups
Java API
Pattern static final
matches anchors, find does not
split takes regex, use quote
w and d are ASCII only
Engine
Backtracking, no timeout
1. What regex is, and why every language has its own
Key idea: a regex is a small language for describing the shape of a string. The core is the same everywhere, but every engine differs in the details, so test in the variant that will actually run it.
You give it a string and a pattern; it gives back matches plus the sub-groups inside each match. The four everyday uses: validation (is this a well-formed order id or email?), search (log lines with ERROR and a code like PAY-\d+), parsing (pull timestamp, user id and voucher id out of a log line), transformation (turn every 2023-09-12 in a file into 12/09/2023).
Java (java.util.regex), JavaScript, PCRE (PHP, Nginx), Python’s re, POSIX (grep, sed) and RE2 (Go) differ in places like: is lookbehind supported, does \w include accented letters, does $ match before a trailing \n, are there possessive quantifiers? Each variant is called a flavor. On regex101, pick the flavor of the place where the regex will run.
2. Core syntax
Key idea: a regex is built from a few bricks: character classes (match one character), quantifiers (repeat), anchors and lookaround (match a position, consume nothing), groups (bundle and remember), flags (change what a few symbols mean).
2.1. Literals, escaping and character classes
Ordinary characters match themselves: car matches “car”. The metacharacters (characters with special meaning) . ^ $ * + ? ( ) [ ] { } | \ must be escaped with \ to match literally: \. is a dot.
A character class [...] matches one character from a set:
| Syntax | Meaning | Example match |
|---|---|---|
[abc] |
one of a, b, c (like a|b|c, but shorter and faster) |
b |
[a-z], [0-9], [a-zA-Z0-9] |
one character in the range | q, 7 |
[^abc] |
one character not in the set (negation) | x, 1 |
[a-z.] |
inside a class, . is a literal dot, no escape needed |
. |
The slides call [abc] an “optional item”; the proper name is character class. ^ at the start of a pattern is an anchor; at the start of a class it means negation.
2.2. Shorthand classes and the dot
| Shorthand | Java default | Negated |
|---|---|---|
\d |
[0-9] |
\D |
\w |
[a-zA-Z_0-9] |
\W |
\s |
[ \t\n\x0B\f\r] |
\S |
\h |
horizontal whitespace (space, tab, …) | \H |
\R |
any line break: \n, \r\n, \r, … |
|
. |
any character except a line terminator |
2.3. Quantifiers
| Quantifier | Meaning |
|---|---|
* |
zero or more |
+ |
one or more |
? |
zero or one |
{n} |
exactly n |
{n,} |
at least n |
{n,m} |
between n and m |
A quantifier applies to the element right before it: ab+ is an a followed by many b’s, (ab)+ is many “ab” pairs. The default is greedy (take as much as possible, then give back); add ? for lazy (*?, +?), or + for possessive (*+, ++). How the three behave inside the engine is the core of part 3.
2.4. Anchors and word boundaries
Anchors match a position and consume no characters:
^: start of input (start of each line withMULTILINE).$: end of input (end of each line withMULTILINE). Without the flag, Java’s$also matches right before a final\n; for the very end use\z.\b: a word boundary,\won one side and not on the other.\bcar\bmatches “car” in “my car” but not inside “carpet”.
Missing anchors are the most common validation bug: \d{6} with find() happily “matches” "abc1234567xyz".
2.5. Groups, alternation and backreferences
(...): a capturing group, bundling tokens for a quantifier or alternation, and remembering the matched text (group(1)).(?<name>...): a named group, read withgroup("name"); much easier to read once there are three or more groups.(?:...): a non-capturing group, grouping only, nothing remembered.|: alternation, match one of the branches. Lowest precedence:^cat|dog$means(^cat)|(dog$), not^(cat|dog)$.\1,\k<name>: a backreference, matching the exact text the group captured.\b(\w+) \1\bfinds doubled words like “is is”.
2.6. Lookaround
Lookaround checks a condition at a position without consuming characters, so what it checks isn’t part of the match:
| Syntax | Name | Meaning |
|---|---|---|
X(?=Y) |
positive lookahead | X, if followed by Y |
X(?!Y) |
negative lookahead | X, if not followed by Y |
(?<=Y)X |
positive lookbehind | X, if preceded by Y |
(?<!Y)X |
negative lookbehind | X, if not preceded by Y |
Example: \d+(?= VND) on “Bob added 2000 VND” matches 2000 without the “ VND”; on “$2000 fly” it matches nothing. In Java a lookbehind must have a bounded maximum length, and the IF-THEN construct the slides mention doesn’t exist.
The IF-THEN conditional and unbounded lookbehind in Java
The slides mention IF condition THEN expression: that’s the conditional (?(1)then|else), available only in PCRE, .NET and Python. Java throws PatternSyntaxException: Unknown inline modifier, and JavaScript doesn’t have it either. In Java, use an alternation or split the job into two patterns.
2.7. Flags
| Flag (JS) | Java | Inline | Effect |
|---|---|---|---|
i |
CASE_INSENSITIVE |
(?i) |
case-insensitive |
m |
MULTILINE |
(?m) |
^ and $ match at the start and end of every line |
s |
DOTALL |
(?s) |
. also matches newlines |
g |
(none) | return every match instead of stopping at the first | |
UNICODE_CHARACTER_CLASS |
(?U) |
Unicode-aware \w, \d, \b |
|
COMMENTS |
(?x) |
ignore whitespace and allow # comments in the pattern |
JavaScript’s g flag and mixing up MULTILINE with DOTALL
3. Regex in Java
Key idea: compile a Pattern once and reuse it; create a fresh Matcher for each match run. The String methods split and replaceAll take a regex, not plain text, so special characters in a delimiter or a replacement are a source of bugs.
3.1. Pattern and Matcher
Pattern is immutable and thread-safe; Matcher holds the state of one matching run, so it isn’t thread-safe.
private static final Pattern ORDER_ID = Pattern.compile("ORD-(?<year>\\d{4})-(?<seq>\\d{6})");
Matcher m = ORDER_ID.matcher("refund ORD-2026-000123 approved");
if (m.find()) {
String year = m.group("year"); // "2026"
String seq = m.group("seq"); // "000123"
int start = m.start(); // where the match begins
}
The three matching methods differ in how much of the input must match:
| Method | Meaning | \d+ on "abc123" |
\d+ on "123abc" |
|---|---|---|---|
matches() |
the whole input must match | false | false |
lookingAt() |
must match from the start, not necessarily to the end | false | true |
find() |
finds the next match anywhere | true | true |
matches() anchors both ends, so it doesn’t need ^...$; validating with find() makes anchors mandatory. Every match as a stream (Java 9+):
List<String> ids = ORDER_ID.matcher(text).results()
.map(r -> r.group(2)) // MatchResult has no group(String) before Java 20
.toList();
3.2. Replacing, quoting and splitting
"2023-09-12".replaceAll("(?<y>\\d{4})-(?<m>\\d{2})-(?<d>\\d{2})", "${d}/${m}/${y}"); // "12/09/2023"
In the replacement string, $1 and ${name} are group references and \ is an escape character.
3.3. Escaping inside string literals
The regex lives inside a string literal, so every regex \ becomes \\: \d+\.\d+ is written "\\d+\\.\\d+", and a single real backslash needs "\\\\".
Long patterns: regex101, text blocks and the COMMENTS flag
- Copy from regex101 (Java flavor) and let the IDE escape on paste (IntelliJ does this).
- Split the pattern into named pieces, or use the
COMMENTSflag to annotate it. - Put multi-line patterns in a text block. A text block does not remove the need to escape
\; it only saves the string concatenation.
private static final Pattern LOG_LINE = Pattern.compile("""
^(?<ts>\\S+)\\s+ # ISO-8601 timestamp
(?<level>INFO|WARN|ERROR)\\s+
(?<pid>\\d+)\\s+
(?<logger>\\w+):\\s
(?<msg>.*)$
""", Pattern.COMMENTS | Pattern.MULTILINE);
With COMMENTS, whitespace in the pattern is ignored; whitespace you actually want to match must be written as \s or \ .
4. “Java’s regex engine doesn’t work well”?
Key idea: that claim is wrong. java.util.regex is a full-featured backtracking engine; what differs from JavaScript is the API and escaping, and the real weakness (backtracking, no timeout) is shared by both.
The slides advise picking the JavaScript engine on regex101 because “Java’s engine doesn’t work well”. In fact Java has things JavaScript only gained recently or still lacks: possessive quantifiers, atomic groups (?>...), \R, \h, \G, \z, \p{...}, class intersection and subtraction ([a-z&&[^aeiou]]). regex101 has a Java 8 flavor: use it.
The real differences between Java and JavaScript
This is where the “Java doesn’t work” feeling comes from:
| Topic | Java | JavaScript |
|---|---|---|
| All matches | find() loop, results() |
g flag, matchAll |
| Validation | matches() anchors the whole string |
you write ^...$ yourself |
| Escaping | \\ in string literals |
/\d/ written directly |
| Lookbehind | must have a bounded maximum length | any length |
\w, \d |
ASCII unless (?U) is on |
ASCII; the u flag doesn’t change \w |
| Possessive, atomic | yes | no possessive; atomic groups not widely available |
| Group references in replacements | $1, ${name} |
$1, $<name> |
Java’s real weakness (shared with JavaScript, PCRE and Python): they are all backtracking engines (try one branch, back up and try another on failure), so some patterns run for a very long time on hostile input, and Java has no built-in timeout. That’s the topic of part 3 and part 4.
Exercises
Every solution was run on JDK 21 (java Exercises.java); the output is shown in each solution.
Exercise 1: Find image file names
Given a list of file names, one per line (.git, students.json, flowers.jpg, deers.png, index.html, river.jpeg, db.sql), find the ones that are images.
Solution to Exercise 1
static final Pattern IMAGE = Pattern.compile(
"^[^/\\s]+\\.(?:jpe?g|png|gif|webp)$",
Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
IMAGE.matcher(files).results().map(MatchResult::group).toList();
// [flowers.jpg, deers.png, river.jpeg]
MULTILINEso^and$anchor per line;CASE_INSENSITIVEsoPHOTO.JPGcounts too.[^/\s]+instead of.+: a file name contains no/or whitespace. The negated class says exactly what’s forbidden, and it can never run past a line boundary.jpe?gcovers bothjpgandjpeg. The extensions sit in(?:...)because we only need grouping, not capturing.- Checking the extension does not prove a file is an image. For uploads, check the magic bytes or use a library such as Apache Tika.
Exercise 2: Find numbers
One string per line: 1, 123, 1234.56, 57890, 1,234.78, 1234A56. Find the lines that are valid numbers: integers, decimals, and numbers with comma thousands separators; reject 1234A56.
Solution to Exercise 2
static final Pattern NUMBER = Pattern.compile(
"^(?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d+)?$", Pattern.MULTILINE);
// [1, 123, 1234.56, 57890, 1,234.78]
The integer part has two branches: with commas it must be proper groups of three digits (1,234, 12,345,678, but not 12,34); without commas it’s \d+. The fractional part (?:\.\d+)? is optional. The anchors are what reject 1234A56: drop them and find() will match the 1234 inside it.
In real code, don’t stop at the regex: after validation, parse with new BigDecimal(s.replace(",", "")). Separators also depend on locale (Vietnamese and German write 1.234,78); for multi-locale input use a NumberFormat for that locale.
Exercise 3: Validate email addresses
Match hoanghai34@gmail.com, re123@ronin-engineer.com, abc.65@example.com, tree@earth.solar.uni; reject admin@, deer@forest, @ronin.dev, a%v@example.com.
Solution to Exercise 3
static final Pattern EMAIL = Pattern.compile(
"^[A-Za-z0-9._-]+@[A-Za-z0-9-]+(?:\\.[A-Za-z0-9-]+)*\\.[A-Za-z]{2,}$");
// first four: true; last four: false
- Local part: letters, digits,
.,_,-, at least one character (rejects@ronin.dev). - Domain: labels joined by dots, with a mandatory TLD of two or more letters (rejects
deer@forestandadmin@).
Remember
- Explain why
.doesn’t match\n, and howMULTILINEdiffers fromDOTALL. - Know when to use
matches(),find()andlookingAt(), and why validating withfind()needs anchors. - Name the three
Stringtraps:split("."),$in areplaceAllreplacement, recompiling the pattern on every call. - Push back on “Java’s engine is bad” and name the real weakness.
- Know the practical way to validate email: a loose regex plus a confirmation email.
Interview questions
Junior
How do matches(), find() and lookingAt() differ?
matches() needs the whole input to match, lookingAt() needs a match from the start, find() finds the next match anywhere.
matches() anchors both ends; lookingAt() doesn’t have to reach the end; find() is called repeatedly for subsequent matches. Use matches() for validation; use find() or results() for search and extraction. Validating with find() and forgetting ^...$ is the classic bug: \d{6} then accepts "abc1234567".
Why does "a.b.c".split(".") return an empty array?
Because the argument of split is a regex, and . matches any character.
Every character is a delimiter, so every piece is empty, and split drops trailing empty strings, leaving an empty array. Fix: split("\\.") or split(Pattern.quote(".")). Same family of traps: |, $, +, *.
When do you use a capturing group, a non-capturing group and a named group?
(...) when you need the matched text back, (?:...) when you only need grouping, (?<name>...) when the pattern has several groups.
(...) groups and remembers the text so you can read it (group(1)) or refer back to it (\1, $1). (?:...) only groups, for a quantifier or alternation over several tokens. (?<name>...) is read with group("name") and referenced as ${name}; adding a new group won’t shift the numbers of the existing ones.
What do ^ and $ mean, and what does MULTILINE change?
By default ^ is the start of the input and $ the end; with MULTILINE, they match at the start and end of every line.
In Java, $ also matches before one final \n; use \z for “the very end”. MULTILINE does not make . match newlines; that’s DOTALL’s job.
Mid
What are lookahead and lookbehind? Give an example that needs them.
Lookaround checks a condition at the current position without consuming characters, so the checked part isn’t in the match.
Example: extracting passport numbers when passport and citizen ids share the same 8-digit format: (?<=passport (?:number|no): )\d{8}. Another: requiring at least one digit in a password with ^(?=.*\d).{8,}$. In Java a lookbehind must have a bounded maximum length; where possible, a capturing group is simpler and more portable.
Why can replaceAll throw when the replacement comes from user data?
Because in the replacement string $n is a group reference and \ is an escape; wrap the value with Matcher.quoteReplacement.
Values like "$5" or "C:\path" throw IndexOutOfBoundsException or IllegalArgumentException, or silently produce the wrong text. If you don’t need regex, use String.replace. The other direction: when user text goes into the pattern, wrap it with Pattern.quote so special characters don’t become regex syntax.
Is Pattern thread-safe? How should you declare it?
Yes: Pattern is immutable, so declare private static final Pattern X = Pattern.compile(...) and share it; Matcher is not thread-safe.
Create a Matcher per use with X.matcher(input), which is cheap. String.matches, replaceAll and split (apart from the single-character fast path) recompile the pattern on every call, so keep them off hot paths.
How do you validate email addresses with regex?
I don’t write an “RFC-correct” regex; I use a loose check and then send a confirmation email.
An RFC-correct regex is enormous, the RFCs themselves disagree in places, and it can’t prove the mailbox exists. I use a loose check (one @, a dot in the domain, a length limit, or Bean Validation’s @Email), normalise (trim, lowercase the domain), then send a confirmation email. I don’t block legal characters like +, because real users rely on me+tag@gmail.com.
Senior
A colleague says “Java’s regex engine is bad, just use JavaScript on regex101”. How do you respond?
Java’s engine isn’t worse; the differences are in the API and escaping, so testing in the JavaScript flavor and pasting into Java is exactly how you get bugs.
java.util.regex is feature-rich (possessive quantifiers, atomic groups, Unicode classes, \R, class intersection); it differs from JavaScript in the API (no g flag, matches() anchors for you), escaping inside string literals, and the lookbehind restriction. Test in regex101’s Java 8 flavor and pin the behaviour with unit tests. The real weakness is backtracking: some patterns run in exponential or high-polynomial time, and Java has no built-in timeout. JavaScript, Python and PCRE share that weakness, so switching flavors doesn’t help; you need careful patterns, input length limits, or a linear-time engine such as RE2/J.
Useful phrases for interviews
- “I always test a pattern in the flavor that will actually run it, and I pin it down with must-match and must-not-match unit tests.”
- “Without anchors,
find()accepts any string that merely contains a match, so for validation I usematches().” - “The dot doesn’t match newlines by default; for multi-line input I either enable
DOTALLor use[\s\S].” - “I prefer named groups because adding a group later doesn’t shift the indices the code relies on.”
- “When the replacement comes from data, I wrap it in
Matcher.quoteReplacement; when the delimiter comes from config, I wrap it inPattern.quote.” - “
Patternis immutable and thread-safe, so it lives in a static final field;Matcheris per call.” - “Java’s engine isn’t worse than JavaScript’s; it’s a different API with the same backtracking risk.”
- “For email, a loose regex plus a confirmation email beats any RFC-complete pattern.”
Notes based on the Advanced Backend course (Ronin Engineer), rewritten in my own words.