Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex how to find the parent match?

Any page from Wikipedia:

...
abas asdn asf asfs af
{{Template1
|a = Name surname
|b = jhsdf sdf
|c = {{Template2}}
|d = 
|e = [[f]] and [[g]]
|h = asd asdasfgasgasg asgas jygh trdx dftf xcth
|i = 73
|j = {{Template2|abc|123}}
|j = {{Template3|aa=kkk|bb={{Template4|cc=uu}}}}
}}

asd wetd gdsgwew g

{{OtherTemplate
|sdf = 213
}}
...

How can i find Template1's content (start is |a end is }}) with Java regexes?

I tried:

String pattern = "\\{\\{\\s*Template1\\s*(.*?)\\}\\}";

Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = p.matcher(content);

while (m.find()) {
    if (!m.group().equals("")) {
        System.out.println(m.group());
        System.out.println("-----------------------");
    }
}

But in here the regex is finding the first }} (which is Template2 }}) then stops.
I want to pass }} is any {{ is open. Then I want to find top parent match.

I want to get top Template1 content between top {{ and }}?.

EDIT:

Please keep in mind that I am parsing content after removing white spaces.

content.replaceAll("\\s+","");  

Think of content as writing a single line.

like image 658
MarsPeople Avatar asked Aug 18 '15 08:08

MarsPeople


People also ask

What is difference between matches () and find () in Java regex?

Difference between matches() and find() in Java RegexThe matches() method returns true If the regular expression matches the whole text. If not, the matches() method returns false. Whereas find() search for the occurrence of the regular expression passes to Pattern.

How do you match parentheses in regex?

The way we solve this problem—i.e., the way we match a literal open parenthesis '(' or close parenthesis ')' using a regular expression—is to put backslash-open parenthesis '\(' or backslash-close parenthesis '\)' in the RE. This is another example of an escape sequence.

How do you denote special characters in regex?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).


1 Answers

/^{{Template1(.*?)^}}/sm

returns:

|a = Name surname
|b = jhsdf sdf
|c = {{Template2}}
|d = 
|e = [[f]] and [[g]]
|h = asd asdasfgasgasg asgas jygh trdx dftf xcth
|i = 73
|j = {{Template2|abc|123}}
|j = {{Template3|aa=kkk|bb={{Template4|cc=uu}}}}

https://regex101.com/r/qC6cM1/1 (DEMO)

like image 153
Trung Nguyen Avatar answered Sep 28 '22 14:09

Trung Nguyen