我收到java.lang.IllegalArgumentException:以下程序的非法团体参考public static void main(String args[]) { String finalQueryString = "TAC: (dash_board or platform* or interfac* or portal* or app or (computer w5 (application or instruction*)) or program or software) and (educat* or ((work* or workforce or employment or job or career) w5 (skill or profile or expertise or abilit* or proficien* or competence or experience)) or train* or certifi*) and ((rank* or scor* or grad* or rate* or rating) w9 (educat* or skill or profile or expertise or abilit* or proficien* or competence or train* or certifi*)) and AC: (G06Q10/063112 or G06Q10/1053$) "; System.out.println(String.format("(%s)", finalQueryString.trim())); String matchedValue = "L12"; System.out.println(String.format("((?<!\\w)%s(?!\\w))", matchedValue)); String parsedQuery = "SC:L12 not SC:L11"; parsedQuery = parsedQuery.replaceAll(String.format("((?<!\\w)%s(?!\\w))", matchedValue), String.format("(%s)", finalQueryString.trim())); System.out.println(parsedQuery);}在 parsedQuery Line 中,我收到非法组引用异常,我无法弄清楚为什么会发生这种情况,有人可以弄清楚吗?
1 回答
暮色呼如
TA贡献1853条经验 获得超9个赞
在替换字符串中,$
是一个特殊字符:它用于从要替换的模式中获取匹配的组
所以你可以使用Matcher.quoteReplacement()
你的替换字符串,如:
parsedQuery = parsedQuery.replaceAll(String.format("((?<!\\w)%s(?!\\w))", matchedValue), Matcher.quoteReplacement(String.format("(%s)", finalQueryString.trim())));
quoteReplacement()
给出一个literal replacement string
代替regex
添加回答
举报
0/150
提交
取消