Java 连接多个匹配项的正则表达式
我有一个这样的字符串“@@VV 1??28>###。@@VV 3 78??#####”,我想用正则表达式提取由该模式识别的三个组”@@VV.###“。 但如果我用前面的模式语法编译测试字符串,我会将整个测试字符串作为一个组而不是三个组来提取。 如何定义regex字符串并获得三个组Java 连接多个匹配项的正则表达式,java,Java,我有一个这样的字符串“@@VV 1??28>###。@@VV 3 78??#####”,我想用正则表达式提取由该模式识别的三个组”@@VV.###“。 但如果我用前面的模式语法编译测试字符串,我会将整个测试字符串作为一个组而不是三个组来提取。 如何定义regex字符串并获得三个组 public static void main(String[] args) { String INPUT = "@@VV 1 ?? 28 > ##@@VV 3 78 ?? > ##@@VV ?? 5
public static void main(String[] args) {
String INPUT = "@@VV 1 ?? 28 > ##@@VV 3 78 ?? > ##@@VV ?? 5 27 > ##";
String startChars = "@@";
String sepChars = ">";
String endChars = "##";
String REGEX = startChars+"VV .*"+sepChars+".*"+endChars;
Pattern pattern = Pattern.compile(REGEX, Pattern.MULTILINE | Pattern.DOTALL);
// get a matcher object
Matcher matcher = pattern.matcher(INPUT);
//Prints the number of capturing groups in this matcher's pattern.
System.out.println("Group Count: "+matcher.groupCount());
while(matcher.find()) {
System.out.println(matcher.group());
}
}
Expected results:
Group Count: 3
@@VV 1 ?? 28 > ##
@@VV 3 78 ?? > ##
@@VV ?? 5 27 > ##
Actual results:
Group Count: 0
@@VV 1 ?? 28 > ##@@VV 3 78 ?? > ##@@VV ?? 5 27 > ##
尝试使用“惰性”运算符
startChars+"VV .*?"+sepChars+".*?"+endChars
注意*?
下面是一个工作示例 尝试使用“惰性”运算符
startChars+"VV .*?"+sepChars+".*?"+endChars
注意*?
下面是一个工作示例 您缺少捕获组:用括号括起来
正则表达式:
(@@VV.*?>##)
缺少捕获组:用括号括起来
正则表达式:(@@VV.*?>##)
关于Spring#split
?关于Spring#split
??