java.lang.IllegalArgumentException:非法组引用

我收到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 中,我收到非法组引用异常,我无法弄清楚为什么会发生这种情况,有人可以弄清楚吗?


温温酱
浏览 84回答 1
1回答

暮色呼如

在替换字符串中,$是一个特殊字符:它用于从要替换的模式中获取匹配的组所以你可以使用Matcher.quoteReplacement()你的替换字符串,如:parsedQuery&nbsp;=&nbsp;parsedQuery.replaceAll(String.format("((?<!\\w)%s(?!\\w))",&nbsp;matchedValue),&nbsp;Matcher.quoteReplacement(String.format("(%s)",&nbsp;finalQueryString.trim())));quoteReplacement()给出一个literal replacement string代替regex
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java