How can I recursively match a pattern using regular expressions?

The string may look like one of the following:

a(b,c) a(a(b,c),d) a(a(a(a(a(b,c),d),a(e,f)),g),h) etc 

I want to match an unlimited number of "a (x, y)". How can I do this with Regex? Here is what I have:

 \\w\\(((?:\\([a-zA-Z0-9]+\\))|(?:[a-zA-Z0-9]+)),((?:\\([a-zA-Z0-9]+\\))|(?:[a-zA-Z0-9]+))\\) 

It corresponds to only two recursions of "a (x, y)".

+6
source share
5 answers

Java standard regex lib does not support recursion, so you cannot map such common nested constructs to it.

But in flavors that support recursion (Perl, PCRE, .NET, etc.), you can use expressions like:

 \w+(?:\((?R)(?:,(?R))*\))? 
+6
source

The language you describe is not an ordinary language , so it cannot be matched with a regular expression. See lexical analysis (i.e. use a parser)

0
source

2 options - 1) Use Lexical Analysis to match and replace patterns yourself [OR] 2) If you want to stick with Regex, then use some shell programming (or any supporting language) and call it from Java.

0
source

You can also use my regex library https://github.com/florianingerl/com.florianingerl.util.regex , which supports recursive regular expressions! The API is basically the same as java.util.regex, only the required import statements are different, for example.

 Pattern p = Pattern.compile("(?<first>a\\((?<second>(?'first')|[a-zA-Z]),(?'second')\\))"); assert p.matcher("a(a(a(a(a(b,c),d),a(e,f)),g),h)").find(); 
0
source

I think you are looking for something like:

a (x, y) = [az] ([az], [az])

regex = a (x, y) | a (regex | y) | a (x, regex)

Not sure how to do it in the language.

-1
source

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


All Articles