为什么会有java.lang.IllegalArgumentException?
我的程序每次运行时都会打印一个为什么会有java.lang.IllegalArgumentException?,java,regex,exception,illegalargumentexception,regex-group,Java,Regex,Exception,Illegalargumentexception,Regex Group,我的程序每次运行时都会打印一个java.lang.IllegalArgumentException。它用不同的表达式替换某些模式,包括它匹配的模式中的组。它会替换部分图案,然后出现此错误: Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference at java.util.regex.Matcher.appendReplacement(Matcher.java:713)
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);
}
}
}
假设:在匹配的组中,有一个有效的组引用,格式为
“$n”
,其中n
无法匹配原始模式中的任何组
因此出现了错误:“非法组引用”
解决方案:使用“$2”
而不是串联。组(2)
等
Ie代替了写作:
"\\\\pochhammer{"+ pochhammer.group(2) + "}{" + pochhammer.group(3) + "}"
写:
"\\\\pochhammer{$2}{$3}"
旁注:在字符类中不需要转义参数<代码>[^]
与[^\)]
一样有效,而且更易于阅读;) 假设:在匹配的组中,有一个有效的组引用,格式为“$n”
,其中n
无法匹配原始模式中的任何组
因此出现了错误:“非法组引用”
解决方案:使用“$2”
而不是串联。组(2)
等
Ie代替了写作:
"\\\\pochhammer{"+ pochhammer.group(2) + "}{" + pochhammer.group(3) + "}"
写:
"\\\\pochhammer{$2}{$3}"
旁注:在字符类中不需要转义参数<代码>[^]
与[^\)]
一样有效,而且更易于阅读;) $
是java.lang.String.replaceFirst
方法中的一个特殊字符
您应该将$
转换为\\$
您可以从
$
中获得结果。$是java.lang.String.replaceFirst
方法中的一个特殊字符
您应该将$
转换为\\$
您可以从中获得结果,请不要只给我们一堆代码,然后说“怎么了?”。非法组引用。。。这意味着你试图访问一些不存在的东西。另外,为什么绝对没有try/catch语句?您应该能够优雅地处理这类事情。@Rick,对不起,只是我无法找出问题的原因。@arco444但这没有意义,因为在匹配和替换了一半文件后,异常只出现了一半。如果这些群体不存在,它可能就不会符合这种模式。现在我将尝试使用try-catch语句。请不要只给我们一堆代码,然后说“怎么了?”。非法组引用。。。这意味着你试图访问一些不存在的东西。另外,为什么绝对没有try/catch语句?您应该能够优雅地处理这类事情。@Rick,对不起,只是我无法找出问题的原因。@arco444但这没有意义,因为在匹配和替换了一半文件后,异常只出现了一半。如果这些群体不存在,它可能就不会符合这种模式。现在我将尝试使用try-catch语句。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会无效。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,则仅链接的答案可能无效。