输入->输入!RC + Calc!R [1] C [1]
在输出中,我要对这些进行操作:
RC和R [1] C [1]
我的尝试:
私有静态无效findMatch(String公式){ 匹配m = Pattern.compile(“ \\ W(R(\\ [(。+?)\\]]?C(\\ [(。+?)\\]]) )“). matcher(formula); //匹配器m = Pattern.compile( //“ \\ W(R(\\ [(。+?)\\]]?C)| \\ W(R(\\ [(。+?)\\ ])?C(\\ [(。+?)\\]))“) // .matcher(formula); 对于(; m.find(); m.reset(formula)){ System.out.println(m.group(3)); } }
它也不会寻找第二种模式,它会进入无限循环。
这怎么了?
请尝试以下操作:
String formula = "Input!RC + Calc!R[1]C[1]";
Matcher m = Pattern.compile("\\W(R(\\[(.+?)\\])?C(\\[(.+?)\\])?)").matcher(formula);
while (m.find()) {
System.out.println(m.group(1));
}
输出:
RC
R[1]C[1]
这里的主要变化是循环的工作方式,我上面的内容是迭代模式匹配的典型方法。我也在打印m.group(1)
而不是m.group(3)
因为它是第一个将包含整个匹配项的组!
,但我认为这是您想要的。
此处对正则表达式的唯一更改是?
在第二(\\[(.+?)\\])
组之后添加了,以使其成为可选。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句