有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java用变化的替换字符串替换子字符串

我正试图编写一个小程序,检测代码文件中的注释,并通过索引标记对其进行标记,这意味着一个具有递增值的标记
例如,此输入:

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) 个答案

  1. # 1 楼答案

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

        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. # 2 楼答案

    要替换检测到的模式,应该使用Matcher#appendReplacement方法来填充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扫描整个字符串,用<TAG_0>...</TAG>替换每个匹配的模式。实际上,循环只执行一次

  3. # 3 楼答案

    菲奇尔特斯特。txt:

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

    应用程序。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());
        }
    
    }
    

    系统。退出:

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