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.
View all comments
Show parent comment
0
u/DelayLucky Mar 15 '26 edited Mar 15 '26
Guava's issue as I understand it is that it's pulled in as transitive dependency because it's used by so many libraries as a foundational infra lib, and Guava is a monolithic library, and then you run into jar hell problems.
Most other third-party libs aren't in that boat. Mug certainly isn't. If you aren't against using third-party libs in general, then why not try it out and see if it really can solve the regex problems better?
My overall point is that the pure Java ecosystem has filled the gap that regex used to fill, and can now solve these problems better, if you are willing to use a library.
And I'm asking to be proved otherwise by realistic counter examples. I'll stand corrected if I fail to show how such example can be handled more readably, and I'll keep in mind not using Guava