Java Regular Expression API

I am writing an application that should define and compile templates at runtime. Using the Java Pattern API, I need to pass a string and get a pattern. Something like that:

 Pattern.compile("ab*|c*"); 

The problem is that my templates are modular, and I would like to compose them using an alternative, kleene star, etc., for example:

 Char a = new Char('a'); Char b = new Char('b'); Char c = new Char('c'); Regex r = new Regex(new Alt(new Seq(a, new KleeneStar(b)), new KleeneStar(c))); Pattern.compile(r); 

I did not find the API in the JDK to allow me such a thing. I believe that the basic template implementation should have such an API. Does anyone know how I can get such an API from standard Java, or are there third-party libraries for this?

In the end, I think I can create such an API myself with some recursive visitors to print a string, but it would be nice if it already exits.

+6
source share
1 answer

VerbalExpressions looks the way you describe. ab*|c* will be expressed as something like this:

 VerbalExpression.regex() .then("a").then("b").zeroOrMore() .or("c").zeroOrMore() .build() 

It also exists in different languages .

0
source

Source: https://habr.com/ru/post/980060/


All Articles