通过更改替换字符串替换子字符串

时间:2015-05-13 20:51:38

标签: java regex replace

我正在尝试编写一个小程序来检测代码文件中的注释,并用index-tag标记它们,这意味着标记的值越来越大。
例如,这个输入:

method int foo (int y) { 
    int temp; // FIRST COMMENT
    temp = 63; // SECOND COMMENT
    // THIRD COMMENT
}

应该改为:

method int foo (int y) { 
    int temp; <TAG_0>// FIRST COMMENT</TAG>
    temp = 63; <TAG_1>// SECOND COMMENT</TAG>
    <TAG_2>// THIRD COMMENT</TAG>
}

我尝试了以下代码:

    String prefix, suffix;
    String pattern = "(//.*)";

    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(fileText);

    int i = 0;
    suffix = "</TAG>";

    while (m.find()) {
        prefix = "<TAG_" + i + ">";
        System.out.println(m.replaceAll(prefix + m.group() + suffix));
        i++;
    }

上述代码的输出是:

method int foo (int y) { 
    int temp; <TAG_0>// FIRST COMMENT</TAG>
    temp = 63; <TAG_0>// SECOND COMMENT</TAG>
    <TAG_0>// THIRD COMMENT</TAG>
}

3 个答案:

答案 0 :(得分:2)

要替换检测到的模式的出现,您应该使用填充Matcher#appendReplacement的{​​{1}}方法:

StringBuffer

StringBuffer sb = new StringBuffer(); while (m.find()) { prefix = "<TAG_" + i + ">"; m.appendReplacement(sb, prefix + m.group() + suffix); i++; } m.appendTail(sb); // append the rest of the contents 进行错误替换的原因是,replaceAll会扫描整个字符串,将每个匹配的模式替换为Matcher。实际上,循环只执行一次。

答案 1 :(得分:0)

您是否尝试过每行读取文件,例如:

    String prefix, suffix;
    suffix = " </TAG>";
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        int i = 0;
        for (String line; (line = br.readLine()) != null;) {
            if (line.contains("//")) {
                prefix = "<TAG_" + i + ">//";
                System.out.println(line.split("//*")[0] + " " + prefix +  line.split("//*")[1] + suffix);
                i++;
            }
         }

} catch (IOException e) {
}

答案 2 :(得分:0)

fichiertexte.txt:

method int foo (int y) { 
    int temp; // FIRST COMMENT
    temp = 63; // SECOND COMMENT
    // THIRD COMMENT
}

App.java:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class App {

    public static void main(String[] args) {

        String fileText = "";
        String fichier = "fichiertexte.txt";

        // lecture du fichier texte
        try {
            InputStream ips = new FileInputStream(fichier);
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String ligne;
            while ((ligne = br.readLine()) != null) {
                //System.out.println(ligne);
                fileText += ligne + "\n";
            }
            br.close();
        } catch (Exception e) {
            System.err.println(e.toString());
        }

        String prefix, suffix;
        String pattern = "(//.*)";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(fileText);

        int i = 0;
        suffix = "</TAG>";

        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            prefix = "<TAG_" + i + ">";
            m.appendReplacement(sb, prefix + m.group() + suffix);
            i++;
        }
        System.out.println(sb.toString());
    }

}

System.out:

method int foo (int y) { 
    int temp; <TAG_0>// FIRST COMMENT</TAG>
    temp = 63; <TAG_1>// SECOND COMMENT</TAG>
    <TAG_2>// THIRD COMMENT</TAG>
}
相关问题