Java 正则表达式-需要帮助
我有一个字符串模板,需要从中获取elseif块的列表。例如,第一个elseif块将来自Java 正则表达式-需要帮助,java,regex,pattern-matching,Java,Regex,Pattern Matching,我有一个字符串模板,需要从中获取elseif块的列表。例如,第一个elseif块将来自 #elseif ( $variable2 )Some sample text after 1st ElseIf. ,第二个elseif块来自elseif$variable2此文本可以重复多次,直到调用do while为止。第二艾尔塞夫 等等。我使用下面的正则表达式 String regexElseIf="\\#elseif\\s*\\((.*?)\\)(.*?)(?:#elseif|#else|#endif
#elseif ( $variable2 )Some sample text after 1st ElseIf.
,第二个elseif块来自elseif$variable2此文本可以重复多次,直到调用do while为止。第二艾尔塞夫
等等。我使用下面的正则表达式
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.";
将匹配从块中的第一个elseif到最近的else或endif(但不包括最近的else或endif)的所有内容
如果然后需要从该匹配中提取单个“elseif”块,请使用
#elseif\b(?:(?!#elseif\b).)*
根据上面第一个正则表达式匹配的结果。在Java中:
Pattern regex = Pattern.compile("#elseif\\b(?:(?!#elseif\\b).)*", Pattern.DOTALL);
等等
将匹配从块中的第一个elseif到最近的else或endif(但不包括最近的else或endif)的所有内容
如果然后需要从该匹配中提取单个“elseif”块,请使用
#elseif\b(?:(?!#elseif\b).)*
根据上面第一个正则表达式匹配的结果。在Java中:
Pattern regex = Pattern.compile("#elseif\\b(?:(?!#elseif\\b).)*", Pattern.DOTALL);
等等。这里的大问题是你需要其他的。。同时作为正则表达式中的开始和停止标记。第一个匹配是子字符串
#elseif ( $variable2 )Some sample text after 1st ElseIf.#elseif($variable2)
然后它开始寻找序列之后的下一个匹配项。因此它将错过第一个if表达式中的第二个elseif,因为elseif$variable2序列已经是前一个匹配的一部分
我将尝试拆分模式\\elseif\\s*\\.*上的字符串:
现在,所有从temp[1]开始的temp条目的开头都有一个elseif块。关于?:else | endif的另一个拆分应该为您提供只包含纯文本的字符串:
for (String s:temp)
System.out.println(s.split("(?:#else|#endif)")[0]);
无法测试第二次拆分,如果它不起作用,则仅将其视为策略建议 这里最大的问题是你还需要别的。。同时作为正则表达式中的开始和停止标记。第一个匹配是子字符串
#elseif ( $variable2 )Some sample text after 1st ElseIf.#elseif($variable2)
然后它开始寻找序列之后的下一个匹配项。因此它将错过第一个if表达式中的第二个elseif,因为elseif$variable2序列已经是前一个匹配的一部分
我将尝试拆分模式\\elseif\\s*\\.*上的字符串:
现在,所有从temp[1]开始的temp条目的开头都有一个elseif块。关于?:else | endif的另一个拆分应该为您提供只包含纯文本的字符串:
for (String s:temp)
System.out.println(s.split("(?:#else|#endif)")[0]);
无法测试第二次拆分,如果它不起作用,则仅将其视为策略建议 此代码
Pattern regexp = Pattern.compile("#elseif\\b(.*?)(?=#(elseif|else|endif))");
Matcher matcher = regexp.matcher(template);
while (matcher.find())
System.out.println(matcher.group());
将产生
#elseif ( $variable2 )Some sample text after 1st ElseIf.
#elseif($variable2)This text can be repeated many times until do while is called. SECOND ELSEIF
#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
秘密在于?=elseif | else | endif,因此将匹配elseif、else或endif,但不会使用字符。这样,它们就可以在下一次迭代中找到。这段代码
Pattern regexp = Pattern.compile("#elseif\\b(.*?)(?=#(elseif|else|endif))");
Matcher matcher = regexp.matcher(template);
while (matcher.find())
System.out.println(matcher.group());
private static final Pattern REGEX = Pattern.compile(
"#elseif\\s*\\(([^()]*)\\)(.*?)(?=#elseif|#else|#endif)");
public static void main(String[] args) {
Matcher matcher = REGEX.matcher(template);
while (matcher.find()) {
System.out.println(matcher.group(2));
}
}
将产生
#elseif ( $variable2 )Some sample text after 1st ElseIf.
#elseif($variable2)This text can be repeated many times until do while is called. SECOND ELSEIF
#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
秘密在于?=elseif | else | endif,因此将匹配elseif、else或endif,但不会使用字符。这样,它们就可以在下一次迭代中找到。可能重复上一篇文章,只返回一个匹配项。我需要得到all elseif blocksI认真地说,我不认为正则表达式是这样解析的合适工具。创建一个语法并使用ANTLR。上一篇文章的可能副本是只返回一个匹配项。我需要得到all elseif blocksI认真地说,我不认为正则表达式是这样解析的合适工具。创建语法并使用ANTLR。
private static final Pattern REGEX = Pattern.compile(
"#elseif\\s*\\(([^()]*)\\)(.*?)(?=#elseif|#else|#endif)");
public static void main(String[] args) {
Matcher matcher = REGEX.matcher(template);
while (matcher.find()) {
System.out.println(matcher.group(2));
}
}