我收到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
提交
取消