我运行时,我的程序正在打印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);
}
}
}
答案 0 :(得分:6)
假设:匹配组中的某个位置,"$n"
格式中存在有效的群组引用,其中n
无法与原始Pattern
中的任何群组匹配。
因此错误:“非法组参考”。
解决方案:使用"$2"
而不是连接.group(2)
等。
"\\\\pochhammer{"+ pochhammer.group(2) + "}{" + pochhammer.group(3) + "}"
写:
"\\\\pochhammer{$2}{$3}"
旁注:不需要在字符类中转义parens; [^)]
与[^\)]
的效果一样,并且更容易阅读;)
答案 1 :(得分:0)