我正在尝试执行以下操作:
Matcher m = Pattern.compile(AMReg.LocLink.regEx()).matcher(input);
if(m.find()) {
urlStr = URLEncoder.encode(m.group(AMReg.LocLink.getPlace(1)), "US-ASCII");
}
AMReg 是一个枚举。 regEx()
返回要搜索的正则表达式,getPlace(x)
返回通常在replaceAll 中使用的组编号,因此它是 $1
或 $2
而不是 m.group(x) 中使用的标准整数;
是否可以使用$2
字段从Matcher返回匹配的组? I searched through the docs ,但找不到方法。
(另外,$2
组号叫什么?使用 $?)
最佳答案
您可以使用Matcher.group (int groupNum)
:
m.group ( AMReg.LocLink.getPlace() );
其中 AMReg.LocLink.getPlace()
返回组 # 1、2 等
关于java - 是否可以使用 $x 从 Matcher 获取组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261867/