正则表达式替换为增量

时间:2015-05-12 11:27:23

标签: java regex

我使用myString.replaceAll("(Chapter.*?)br","Chapter **INCREMENT**");

我想粘贴一个**increment**而不是INCREMENT,但我不知道我该怎么做。

我有下一个文字:

  

第一章br

     

TextTextTextA

     

第B345章br

     

TextTextTextB345

     

第AB章br

     

TextTextTextAB

它必须看起来像:

  

第1章br

     

TextTextTextA

     

第2章br

     

TextTextTextB345

     

第3章br

     

TextTextTextAB

1 个答案:

答案 0 :(得分:2)

我有不同索引的章节。例如,A章和B章。我正在用正则表达式抓住所有章节,并将唯一的章节改为章节+增量。

找到解决方案:

int n = 0;
Pattern r = Pattern.compile("(Chapter.*?)<br />");

Matcher m = r.matcher(fileToShow);
while (m.find()) {
    System.out.println("Found value: " + m.group(0));
    fileToShow = fileToShow.replaceAll(m.group(0),"<Chapter "+ ++n +">");
}