Jason4Ever
Jason4Ever

Reputation: 1479

How to extract elements between mulitple curly braces

I'm trying to write a Regex on Java for extracting the elements between double curly braces

Example :

I am very much new to java...{{envelope|func"{{aa}}{{ds}}|{{abc.xyz}}"}} and i want to know.... more.  

I tried this regex but it doesn't return the first element full

{\{(.*?)(?:\{\{(.*?)\}\})*\}\}

I want to extract as the following :

Upvotes: 3

Views: 156

Answers (1)

anubhava
anubhava

Reputation: 785058

Converting my comment to an answer. This is assuming between {{ and }} we have all non-whitespace characters.

This regex is based on lookahead and alternation inside the lookahead. Captured groups are part of lookahead itself.

\{\{(?=((?:(?!\{\{|}})\S)*+|\S*)}})

Or in Java:

final String regex = "\\{\\{(?=((?:(?!\\{\\{|\\}})\\S)*+|\\S*)\\}})";

RegEx Demo

Upvotes: 2

Related Questions