我的程序每次运行时都会打印java.lang.IllegalArgumentException
。它使用不同的表达式替换某些模式,包括它匹配的模式中的组。它取代了图案的一部分,那么这个错误出现:为什么会有java.lang.IllegalArgumentException?
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)
这是我的代码:
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);
}
}
}
结果请不要只是手我们一串代码,并说:“请告诉我错了吗?”。 – 2014-03-03 00:32:37
非法群组引用...这意味着您正试图访问不存在的内容。另外,为什么try/catch语句绝对没有?你应该能够在一定程度上处理这些事情。 – arco444
@瑞克,我很抱歉,只是我无法弄清楚可能导致问题的原因。 – user2825125