r/java • u/DelayLucky • Mar 07 '26
Build Email Address Parser (RFC 5322) with Parser Combinator, Not Regex.
A while back, I was discussing with u/Mirko_ddd, u/jebailey and u/Dagske about parser combinator API and regex.
My view was that parser combinators should and can be made so easy to use such that it should replace regex for almost all use cases (except if you need cross-language portability or user-specified regex).
And I argued that you do not need a regex builder because if you do, your code already looks like a parser combinator, with similar learning curve, except it doesn't enjoy the strong type safety, the friendly error message and the expressivity of combinators.
I've since used the Dot Parse combinator library to build a email address parser, following RFC 5322, in 20 lines of parsing and validation code (you can check out the makeParser() method in the source file).
While light-weight, it's a pretty capable parser. I've had Gemini, GPT and Claude review the RFC compliance and robustness. Except the obsolete comments and quoted local part (like the weird "this.is@my name"@gmail.com) that were deliberately left out, it's got solid coverage.
Example code:
EmailAddress address = EmailAddress.parse("J.R.R Tolkien <tolkien@lotr.org>");
assertThat(address.displayName()).isEqualTo("J.R.R Tolkien");
assertThat(address.localPart()).isEqualTo("tolkien");
assertThat(address.domain()).isEqualTo("lotr.org");Benchmark-wise, it's slightly slower than Jakarta's hand-written parser in InternetAddress; and is about 2x faster than the equivalent regex parser (a lot of effort were put in to make sure Dot Parse is competitive against regex in raw speed).
To put it in picture, Jakarta InternetAddress spends about 700 lines to implement the tricky RFC parsing and validation (link). Of course, Jakarta offers more RFC coverage (comments, and quoted local parts). So take a grain of salt when comparing the numbers.
I'm inviting you guys to comment on the email address parser, about the API, the functionality, the RFC coverage, the practicality, performance, or at the higher level, combinator vs. regex war. Anything.
Speaking of regex, a fully RFC compliant Regex (well, except nested comments) will likely be more about 6000 characters.
This file (search for HTML5_EMAIL_PATTERN) contains a more practical regex for email address parsing (Gemini generated it). It accomplishes about 90% of what the combinator parser does. Although, much like many other regex patterns, it's subject to catastrophic backtracking if given the right type of malicious input.
It's a pretty daunting regex. Yet it can't perform the domain validation as easily done in the combinator.
You'll also have to translate the quoted display name and unescape it manually, adding to the ugliness of regex capture group extraction code.
View all comments
Show parent comment
1
u/Mirko_ddd Mar 10 '26
I'm actually a chemical engineer by education (high school diploma, not a college degree, so not an engineer, but I don't know how to translate that into English) who entered the tech world as a hobby and currently works as a freelance Android developer (an entrepreneur, I make and distribute my own apps). So I was thinking about something chemistry-related; parsing and data mining chemical formulas from unstructured text is exactly the kind of real-world scenario where regular expressions really shine, in my opinion.
So here is the challenge: extract valid hydroxides from a chemistry textbook, while ignoring the false positives and typos.
The rules of the domain:
Na,Ca,Fe).OH) or complex with parentheses and a Unicode subscript ((OH)₂,(OH)₃).₁₂).CaOH₂(missing parentheses are invalid) and avoid partial matches. We also must ignore plain words likeOH!.With standard string combinators, handling Unicode ranges natively, word boundaries, and avoiding partial matches usually requires building a complex state machine or a full-blown lexer. With regex (using Sift) I can express this domain logic declaratively:
When I run this against a text like: "Sodium forms NaOH, calcium forms Ca(OH)₂. Exclamations like OH! are ignored. Typos like CaOH₂ are rejected. Double digits like Xy(OH)₁₂ pass."
Regex effortlessly extracts exactly
["NaOH", "Ca(OH)₂", "Xy(OH)₁₂"].The real killer features here are the Word Boundaries
\b(to isolate the molecules from normal text) and the Negative Lookahead(?!...)(to say "only accept simple OH if there is NO subscript ahead", without actually consuming the next character).I'd genuinely love to see how your combinator library handles the lookaround assertions and dynamic Unicode ranges without the code becoming deeply nested or requiring manual character-by-character consumption. Let me know what you think!
Actually, this use case turned out so well that I'm going to add it as a dedicated recipe in Sift's official
COOKBOOK.md.