Hi All,
I've a String template which I need to process using regex. I need to get the list of #if #endif
blocks from the below template. I tried the following regular expression
String regexIfEndIf="\\#if(.*)\\#endif";
But the below code
Pattern pattern=Pattern.compile( regexIfEndIf,Pattern.DOTALL);
Matcher matcher=pattern.matcher( template );
while(matcher.find()){
System.out.println("Found a match:[" + matcher.group()+"]");
}
The above system out prints everything from first #if
to last #endif
. But I need to get two blocks. ie first matcher.find()
should find the first #if - #endif
block and second matcher.find()
should find the second #if-#endif
. Please help me to fix the regex to return blocks of #if-#endif
blocks. Below is the template used.
String template =
"This is a sample document."
+ "#if ( $variable1 )"
+ "FIRST This text can be repeated many times until do while is called."
+ "#elseif ( $variable2 )"
+ "Some sample text after 1st ElseIf."
+ "#elseif($variable2)"
+ "This text can be repeated many times until do while is called. SECOND ELSEIF"
+ "#else "
+ "sample else condition "
+ "#endif "
+ "Some sample text."
+ "This is the second sample document."
+ "#if ( $variable1 )"
+ "SECOND FIRST This text can be repeated many times until do while is called."
+ "#elseif ( $variable2 )"
+ "SECOND Some sample text after 1st ElseIf."
+ "#elseif($variable2)"
+ "SECOND This text can be repeated many times until do while is called. SECOND ELSEIF"
+ "#else " + "SECOND sample else condition " + "#endif "
+ "SECOND Some sample text.";