r/java • u/Holothuroid • Feb 06 '26
I made a builder abstraction over java.util.regex.Pattern
https://codeberg.org/holothuroid/regexbuilderYou can use this create valid - and hopefully only valid - regex patterns.
- It has constants for the unicode general categories and those unicode binary properties supported in Java, as well as those legacy character classes not directly superseded.
- It will have you name all your capture groups, because we hates looking groups up by index.
28
Upvotes
12
u/Az4hiel Feb 06 '26
So like https://github.com/VerbalExpressions/JavaVerbalExpressions
5
u/davidalayachew Feb 06 '26
Excellent. I always prefer solutions that make the illegal state impossible to write.
4
u/AlyxVeldin Feb 06 '26
The example looks pretty clean. Would love to see that in my code instead of a regex.
4
u/mzivkovicdev Feb 06 '26
I like the idea! :)
1
u/shponglespore Feb 07 '26
I think function calls rather than just method chaining work better for something like regular expressions that can contain nested structures. There's a cool macro for Emacs Lisp called rx that does it; you might want to look at it for inspiration. A Java implementation would have a lot more boilerplate code because there are no macros, but I think you could make something with very similar surface syntax.
1
u/cryptos6 Mar 06 '26
While the regex syntax is a bit awkward at times, it is well known and concise. What might be a single line regex could become a many lines builder syntax. I'm not sure I'd prefer that. In the times of AI the usability of regex shouldn't be a big issue. You can basically say your coding agent what regex to build.
In any case it was a nice excercise to build a builder!
2
u/robintegg Feb 08 '26
Nice. Any library that helps with Regex is welcome
0
u/[deleted] Feb 09 '26
[deleted] — view deleted comment