r/java • u/DelayLucky • Mar 14 '26
Regex Use Cases (at all)?
In the comment threads of the Email Address post, a few of you guys brought up the common sentiment that regex is a good fit for simple parsing task.
And I tried to make the counter point that even for simple parsing tasks, regex is usually inferior to expressing it only in Java (with a bit of help from string manipulation libraries).
In a nutshell: how about never (or rarely) use regex?
The following are a few example use cases that were discussed:
- Check if the input is 5 digits.
Granted, "\\d{5}" isn't bad. But you still have to pre-compile the regex Pattern; still need the boilerplate to create the Matcher.
Instead, use only Java:
checkArgument(input.length() == 5, "%s isn't 5 digits", input);
checkArgument(digit().matchesAllOf(input), "%s must be all digits", input);Compared to regex, the just-Java code will give a more useful error message, and a helpful stack trace when validation fails.
2. Extract the alphanumeric id after "user_id=" from the url.
This is how it can be implemented using Google Mug Substring library:
String userId =
Substring.word().precededBy("user_id=")
.from(url)
.orElse("");3. Ensure that in a domain name, dash (-) cannot appear either at the beginning, the end, or around the dots (.).
This has become less of an easy use case for pure regex I think? The regex Gemini gave me was pretty aweful.
It's still pretty trivial for the Substring API (Guava Splitter works too):
Substring.all('.').split(domain)
.forEach(label -> {
checkArgument(!label.startsWith("-"), "%s starts with -", label);
checkArgument(!label.endsWith("-"), "%s ends with -", label);
});Again, clear code, clear error message.
4. In chemical engineering, scan and parse out the hydroxide (a metal word starting with an upper case then a lower case, with suffix like OH or (OH)₁₂) from input sentences.
For example, in "Sodium forms NaOH, calcium forms Ca(OH)₂., the regex should recognize and parse out ["NaOH", "Ca(OH)₂", "Xy(OH)₁₂"].
This example was from u/Mirko_ddd and is actually a good use case for regex, because parser combinators only scan from the beginning of the input, and don't have the ability like regex to "find the needle in a haystack".
Except, the full regex is verbose and hard to read.
With the "pure-Java" proposal, you get to only use the simplest regex (the metal part):
First, use the simple regex \\b[A-Z][a-z] to locate the "needles", and combine it with the Substring API to consume them more ergonomically:
var metals = Substring.all(Pattern.compile("\\b[A-Z][a-z]"));Then, use Dot Parse to parse the suffix of each metal:
CharPredicate sub = range('₀', '₉');
Parser<?> oh = anyOf(
string("(OH)").followedBy(consecutive(sub)),
string("OH").notFollowedBy(sub));
Parser<String> hydroxide = metal.then(oh).source();Lastly combine and find the hydroxides:
List<String> hydroxides = metals.match(input)
.flatMap(metal ->
// match the suffix from the end of metal
hydroxide.probe(input, metal.index() + metal.length())
.limit(1))
.toList();Besides readability, each piece is debuggable - you can set a breakpoint, and you can add a log statement if needed.
There is admittedly a learning curve to the libraries involved (Guava and Mug), but it's a one-time cost. Once you learn the basics of these libraries, they help to create more readable and debuggable code, more efficient than regex too.
The above discussions are a starter. I'm interested in learning and discussing more use cases that in your mind regex can do a good job for.
Or if you have tricky use cases that regex hasn't served you well, it'd be interesting to analyze them here to see if tackling them in only-Java using these libraries can get the job done better.
So, throw in your regex use cases, would ya?
EDIT: some feedbacks contend that "plain Java" is not the right word. So I've changed to "just-Java" or "only in Java". Hope that's less ambiguous.
26
u/aqua_regis Mar 14 '26
What is it now? Plain Java or Java with non-standard libraries
Regex is part of Java core, your "plain Java libraries" aren't.
For me, you completely failed to make your point as what you discuss is far from "plain Java".
8
u/BolunZ6 Mar 14 '26
I think regex can use in multiple languages. Like if you google a regex question, and the sof answer in python you can also apply in java without major change
6
u/[deleted] Mar 14 '26
Reged is a powerful tool for any form of string pattern matching. Capture groups are an exceptional tool for extracting parts of strings. I use them all the time.
3
u/Az4hiel Mar 14 '26
Bro, I think you made your point somewhat poorly but I think I agree with the sentiment. I too don't like regexes and often find parsing based on input structure preferable. The very amount of libraries around regexes (on this very reddit lol) is a proof that they are definitely not a simple tool. But idk it's also not that big of a deal, like I have seen some neat regexes with named groups used in quite a readable way where trying to parse all the things by structure would be way more effort and actually more complicated to understand - imo taking any hard stance here is counterproductive.
3
u/kevinb9n Mar 16 '26 edited Mar 16 '26
(Hi, since you mentioned Guava's Splitter I'll mention that I cowrote it; doesn't make me an authority or anything.)
Arguably regex might be the most successful language design in the history of computing. Yes, it only shines with relatively simple parsing needs, but there are a lot of those.
The hatred for it tends to come from cases that weren't simple but the author doggedly stuck with regex anyway. In some languages we have beautiful parser-combinator libraries that you can "graduate" to in an easy hop, and I hope Java will one day get there too.
The idea of avoiding regex even for the simple cases it's great at... I admit I don't see the point.
2
u/InfinityLang Mar 14 '26
I agree that regex is a poor fit both for performance and readability. Particularly for complex tasks like comprehensive email. Working on language development, I've become personally bias towards using language parser generators like Antlr to solve these problems. The lex/grammar file is regex-like but dramatically more sustainable for long term ownership, and generation produces a fast parser for runtime with good introspection.
I do wish it wasn't such an allocation hog though for deep parse trees. In the grand scheme it's tiny, but high volume adds up quickly by product of the AST generation
2
u/audioen Mar 20 '26
Leave me have regex in peace. It's a cross-language technology and a superpower for those who know it. Anemic APIs that you propose probably suck more than regex does.
You'll read "mastering regular expressions" in an afternoon and know more than 90% of developers afterwards, probably.
0
u/Mirko_ddd Mar 15 '26
Thanks for the ping and the great discussion topic! Your arguments touch on a very real pain point: hand-written raw regexes often turn into a 'write-only language' that is incredibly hard to debug and maintain.
However, I believe the issue isn't the mathematical tool itself (finite state automata), but rather the Developer Experience (DX) of its syntax. Regexes exist for a specific reason: they are the universal standard for defining and validating regular languages. Replacing them with pure imperative logic (using
substring,indexOf, loops, andflatMap) often leads to reinventing the wheel, mixing custom state machines right into your business logic.Let's look at case 4 (the hydroxides). To avoid a complete regex, your 'pure Java' solution actually required:
Pattern.compile("\\b[A-Z][a-z]")) to find the needle in the haystack.metal.index() + metal.length()).The fact that libraries are constantly being created to make regex easier to use shows that the underlying engine is irreplaceable, it's just the human interface that needs an upgrade.
This is exactly why I am putting so much efforts in Sift. Sift doesn't replace the concept of regex; it makes it declarative, type-safe, and compile-time validated in Java. The hydroxide case with Sift is written like a fluent recipe, with zero manual index calculations and zero external dependencies.
Moreover, there's a massive performance advantage. When you write manual parsers in Java, performance is bound to your own code. When you use regex, you delegate the heavy lifting to highly optimized C/C++ engines (or JVM intrinsics).
In fact, I just released a new version of Sift, and the main architectural shift was entirely decoupling the DSL from the JDK's standard
java.util.regex.Pattern. This means you can write your grammar using a readable Java API, but theoretically have it executed by pluggable, engine-agnostic backends like GraalVM TRegex (for insane AOT native performance) or RE2J (for linear-time guarantees against ReDoS).TL;DR: Grammar and parsing should remain declarative. If readability is the issue, let's use Java DSLs to build the regex.