Question

My program is printing a java.lang.IllegalArgumentException whenever I run it. It replaces certain patterns with different expressions including groups from the pattern it matched. It replaces a portion of the patterns, then this error comes up:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
    at java.util.regex.Matcher.appendReplacement(Matcher.java:713)
    at RealReadFile.main(RealReadFile.java:93)

This is my code:

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;

    public RealReadFile() throws FileNotFoundException {
        if (myFile == null)
            myFile = new Scanner(new File(fileName));
    }

    public RealReadFile(String name) throws FileNotFoundException {
        if (myFile != null)
            myFile.close();
        myFile = new Scanner(new File(name));
    }

    public boolean endOfFile() { 
        return !myFile.hasNext(); 
    }

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

    public int times(String oneline){
            int count = 0;
            Pattern cpochhammer = Pattern.compile("(\\(([^\\)]+)\\)_\\{?([^\\}]+)\\}?)");
            Matcher pochhammer = cpochhammer.matcher(oneline);
            while (pochhammer.find()) {
                count++;
            }   
        return count;
    }

    public void multipleChar(RealReadFile file){
        while (!file.endOfFile()) {
            String line = file.nextLine();
            int count=file.times(line);
            while(count>0){
                Pattern cpochhammer = Pattern.compile("(\\(([^\\)]+)\\)_\\{?([^\\}]+)\\}?)");
                Matcher pochhammer = cpochhammer.matcher(line);
                if (pochhammer.find()) {
                    //System.out.println(line);
                    line = pochhammer.replaceFirst("\\\\pochhammer{"+ pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
                    count--;
                    }
                if(count==0)
                    System.out.println(line);
                }
            }
    }

    public void singleChar(RealReadFile file){
        while (!file.endOfFile()) {
            String line = file.nextLine();
            int count=file.times(line);
            while(count>0){
            Pattern cpochhammer = Pattern.compile("(\\(([^\\)]+)\\)_(.))");
            Matcher pochhammer = cpochhammer.matcher(line);
            if (pochhammer.find()) {
                //System.out.println(line);
              line = pochhammer.replaceFirst("\\\\pochhammer{"
                        + pochhammer.group(2) + "}{" + pochhammer.group(3)
                        + "}");
                count--;
            }
            if(count==0)
                System.out.println(line);
            }
            }
    }
    public boolean checkMultiple(String line){
        Pattern cpochhammer = Pattern.compile("(\\(([^\\)]+)\\)_\\{([^\\}]+)\\})");
        Matcher pochhammer = cpochhammer.matcher(line);
        if(pochhammer.find())
            return true;
        return false;
    }

    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);
            StringBuffer rplcmntBfr = new StringBuffer();
            while(pochhammer.find())  {
               pochhammer.appendReplacement(rplcmntBfr, "\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
            }
            pochhammer.appendTail(rplcmntBfr);
            System.out.println(rplcmntBfr);
        }
    }
}
Was it helpful?

Solution

Hypothesis: somewhere in your matching groups, there is a valid group reference in the form "$n" where n cannot match any group in the original Pattern.

Hence the error: "illegal group reference".

Solution: use "$2" instead of concatenating .group(2) etc.

Ie instead of writing:

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

write:

"\\\\pochhammer{$2}{$3}"

Side note: no need to escape parens in a character class; [^)] works just as well as [^\)], and it is easier to read ;)

OTHER TIPS

$ is a special character in java.lang.String.replaceFirst Method.

You should turn $ into \\$.

You can get the result from http://www.onstepr.com/posts/51

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