Question

When I run this code, it prints out only the groups found in the first pattern it matched per line. However, there are multiple strings I want to replace in each line and I want it to print out the specific groups for each string in the pattern it matched. How can I change it so that it prints out the groups specific to the patterns/strings it found in each line instead of printing only the groups in the first pattern match it found?

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.FileNotFoundException;
import java.io.File;

public class RealReadFile {
    private static final String fileName = "KLSadd.tex";

    private Scanner myFile = null;

    // No-args constructor, create a new scanner for the specific file defined
    // above

    public RealReadFile() throws FileNotFoundException {

        if (myFile == null)
            myFile = new Scanner(new File(fileName));

    }

    // One-arg constructor - the name of the file to open

    public RealReadFile(String name) throws FileNotFoundException {

        if (myFile != null)

            myFile.close();

        myFile = new Scanner(new File(name));

    }
    public boolean endOfFile() {    // Return true is there is no more input

        return !myFile.hasNext();   // hasNext() returns true if there is more input, so I negate it

    }

    public String nextLine() {
        return myFile.nextLine().trim();
    }

    public static void main(String[] args) throws FileNotFoundException {
        RealReadFile file = new RealReadFile();
        while(!file.endOfFile()) {
            String line = file.nextLine();
            Pattern cpochhammer = Pattern.compile("(\\(([^\\)]+)\\)_\\{?([^\\}]+)\\}?)");
            Matcher pochhammer = cpochhammer.matcher(line);
            while (pochhammer.find()){
                System.out.println(line);
                String line2=pochhammer.replaceAll("\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
                System.out.println(line2);
            }
        }
    }
}
Was it helpful?

Solution

You are misunderstanding the purpose of Matcher's find() and replaceAll() functions. It makes no sense to use replaceAll() inside a find() loop:

while (pochhammer.find()){
   System.out.println(line);
   String line2=pochhammer.replaceAll("\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
   System.out.println(line2);
}

To replace all instances, you'll need to change it to something like this:

StringBuffer rplcmntBfr = new StringBuffer();
while(pochhammer.find())  {
   pochhammer.appendReplacement(rplcmntBfr, "\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
}
pochhammer.appendTail(rplcmntBfr);
System.out.println(rplcmntBfr);

Accessing individual match-groups and using replaceAll(s)

pochhammer.replaceAll("\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");

makes no sense, because group(i) is only for use with find(), yet replaceAll(s) is for replacing all matches in the line at once.

Here's a tutorial on making replacements with find(), appendReplacement(sb,s) and appendTail(sb): http://tutorials.jenkov.com/java-regex/matcher.html#appendreplacement-appendtail-methods

You may also want to take a look at this question, regarding the difference between Greedy vs. Reluctant vs. Possessive Quantifiers, as I suspect that the quantifiers in your regex may need to change from +, to either +? or ++. But since I don't know what your input or expected output looks like, I can only guess at this part.

The problem I mention above is the big one.

Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top