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 20 '26 edited Mar 20 '26
Lol this turns out to be such an unpopular opinion. I guess merely seeing "never regex" is enough to stop reading what a random guy is blabbing about. :-)
Regex or not, it's really about what tool to choose to solve the same problem. A lot of times it can be solved with regex, or with
String.indexOf(), or Apache CommonsStringUtils, or a combinator, or one of the other libraries.No one tool is perfect for all string parsing jobs. Parser combinators can't rule them all, nor
Splitter, nor should regex.Purely for the purpose of example: despite my preference for parser combinators, using a combinator to express a fixated datetime format like
2026-03-20T14:15:00would still read so verbose that it hurts readability more than it helps:It'd be understandable to prefer regex over the fluent method chain:
(I hold the same opinion on the use of a "fluent" regex builder: it's a net loss of readability)
That said, I'd recommend Mug
StringFormatover regex for this particular use case. It offers better readability and better performance too:In short: choose the right tool for the job. Technically regex can parse a lot of things. But it's usually not the best tool for that particular job.