Replace substring with replacement string

I am trying to write a small program that detects comments in a code file, and tag them with an index-tag, which means an incrementing tag.
For example, this input:

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

      

should be changed to:

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

      

I tried the following code:

    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++;
    }

      

Output for the above code:

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


source to share


3 answers


To replace the occurrence of detected patterns, you must use a method Matcher#appendReplacement

that fills in 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

      



The reason replaceAll

will result in an incorrect substitution as it will check the Matcher

entire string to replace each matched pattern with <TAG_0>...</TAG>

. In fact, the loop will only run once.

+2


source


Have you tried reading the file in line, for example:



    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) {
}

      

0


source


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>
}

      

0


source







All Articles