Java REGEX appendReplacement()方法在第二个参数中不接受变量

时间:2019-05-24 06:05:58

标签: java regex

我有一个来自文件的以下字符串:

{"comment":"<p>Some text with double quotes like (example, "$", "(", ")", etc.) here in a paragraph.</p>","replies":{"1":{"user":"Some User1","reply":"<p>This is another sample "data" with some double quotes.</p>","lastModified":"2017-04-12T15:03:06Z"},"2":{"user":"SomeUser2","reply":"<p>Yet another "data" with some more double quotes.</p>","lastModified":"2017-04-12T15:03:06Z"}}}

我的目标是在HTML p(段落)标记中转义双引号字符,而我尝试这样做的方式是:

public class Test {

  public static void main(String[] args) {
    FileInputStream fis;
    try {
      fis = new FileInputStream("PATH_TO_FILE");
      String data = IOUtils.toString(fis, "UTF-8");
      System.out.println("ORIGINAL: " + data + "-");

      Pattern pTagPattern = Pattern.compile("(<p>)(.*?)(</p>)");
      Matcher pTagMatcher = pTagPattern.matcher(data);

      String str ;
      Map<Integer, String> map = new HashMap<Integer, String>();
      int i = 1;
      while(pTagMatcher.find()) {
        map.put(i++, StringEscapeUtils.escapeJava(pTagMatcher.group(2)));
      }

      Matcher pTagMatcher1 = pTagPattern.matcher(data);
      StringBuffer sb1 = new StringBuffer();
      int j = 1;
      String str1;
      while(pTagMatcher1.find()) {
        str1 = map.get(j);
        pTagMatcher1.appendReplacement(sb1, str1); //<=== EXCEPTION HERE
        j++;
      }
      pTagMatcher1.appendTail(sb1);
      System.out.println("\nFINAL Text is: "  + sb1.toString());


    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

运行上面的代码时,出现如下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
    at java.util.regex.Matcher.appendReplacement(Unknown Source)
    at my.project.package.util.Test.main(Test.java:54) (The line in the code above where I have "<=== EXCEPTION HERE")

异常在线上

pTagMatcher1.appendReplacement(sb1, str1);

在这里,对于appendReplacement()方法的第二个参数,我试图使用变量而不是实际的文字字符串。当我使用文字字符串(例如“示例文本”)时,它会起作用。

我对Java正则表达式不是很熟悉,因此如果有人可以指出我在这里做错了,将不胜感激。如果有人可以提出另一种更好的方法来做到这一点,那就太好了。

谢谢。

0 个答案:

没有答案