dgliosca
dgliosca

Reputation: 49

How can I match the content inside a couple of parenthesis with regex if there are inner parenthesis?

The problem I want to solve is the following: I have this string as input:

Hello World: [[DATA.[field]]] something else

the group I want to catch is:

DATA.[field]

The problem is that I want to catch whatever is inside "[[" and "]]".

What is the regex I should use in Java? Thanks

Upvotes: 0

Views: 103

Answers (1)

Saleem
Saleem

Reputation: 9018

Try following regex:

(?<=\[\[).*?(?=\]\](\s|$))

Example:

String text = "Hello World: [[DATA.[field]]] something else";

Pattern regex = Pattern.compile("(?<=\\[\\[).*?(?=\\]\\](\\s|$))", Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(text);

if (regexMatcher.find()) {
    System.out.println(regexMatcher.group());
}

From sample data string, it will print

DATA.[field]

Upvotes: 1

Related Questions