【发布时间】:2010-09-30 03:58:36
【问题描述】:
我有一个字符串模板,我需要从中获取#elseif 块的列表。例如,第一个 #elseif 块将来自
#elseif ( $variable2 )Some sample text after 1st ElseIf.
,第二个#elseif块来自#elseif($variable2)This text can be repeated many times until do while is called. SECOND ELSEIF
等等。我为此使用了以下正则表达式。
String regexElseIf="\\#elseif\\s*\\((.*?)\\)(.*?)(?:#elseif|#else|#endif)";
但它只返回一个匹配项,即第一个#elseif 块而不是第二个。我还需要获得第二个#elseif 块。你能帮我这样做吗?请找到以下字符串模板。
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.";
【问题讨论】:
-
Regex Help needed. 的可能重复项
-
上一篇文章只返回一个匹配项。我需要获取所有 elseif 块
-
我真的不认为正则表达式是这样的解析工具。创建语法并使用 ANTLR。
标签: java regex pattern-matching