regex: java.util.regex companion code (groups, lookarounds, replaceAll lambdas, ReDoS and timeouts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KqJyCidz3ZgRyHABv2GVJh
This commit is contained in:
2026-09-30 19:00:39 +00:00
co-authored by Claude Sonnet 5.5
parent bfa7f97432
commit b31c6716a3
22 changed files with 647 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
# regex
Companion code for the ankurm.com post *"Java Regex Tutorial: Pattern, Matcher, Groups, Lookarounds
and ReDoS."* Module `regex` in `java-core-examples`.
All explanation lives in the post; this module holds the runnable evidence and the captured output.
## Versions
| Component | Version |
|---|---|
| JDK | 25.0.4.1+1 (Temurin, LTS) |
| JUnit Jupiter | 5.11.0 |
| Hardware | 2 vCPU x86-64 VM (timings are indicative, not a leaderboard) |
## Quickstart
```bash
export JDK25_HOME=/path/to/jdk-25
./scripts/run-all.sh # rebuilds, runs the tests, regenerates everything in output/
```
## What is in here
| File | Shows | Output |
|---|---|---|
| `BasicsDemo` | `matches` / `lookingAt` / `find`, numbered and named groups, `results()`, flags | `01` |
| `LookaroundDemo` | lookahead, lookbehind, password policy, thousands separators, lookbehind limits | `02` |
| `ReplaceDemo` | `$1` / `${name}`, the `$` trap, `replaceAll(Function)`, `quoteReplacement`, `appendReplacement` | `03` |
| `BlowupDemo` | timings at growing input: what the JDK tames, what it does not | `04` |
| `FixesDemo` | rewrite, possessive, atomic group; `find()` is still quadratic | `05` |
| `LimitsDemo` | `StackOverflowError` from `(?:a\|b)*` on long input | `06` |
| `RegexTimeout`, `Safe` | a `CharSequence` that aborts a match at a deadline; length limit + deadline wrapper | used by `04`, `05` |
| JDK `Pattern.java` excerpt | the loop-memoisation gate, and the absence of any timeout API | `07` |
| `RegexClaimsTest` | 11 assertions behind the claims above | `08` |
Every timing run is capped (2 s in `BlowupDemo`) by `RegexTimeout`, so the demos always finish.
Timings move by tens of percent between runs; the growth shape and which patterns abort do not.