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.
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.
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.
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 ( \ ).
/^{{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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With