gpt4 book ai didi

java - 为什么会出现 java.lang.IllegalArgumentException?

转载 作者:行者123 更新时间:2023-11-29 07:03:52 27 4
gpt4 key购买 nike

我的程序每次运行时都会打印一个 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" 而不是连接 .group(2)

即代替写作:

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

写:

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

旁注:无需在字符类中转义括号; [^)][^\)] 一样好用,而且更容易阅读 ;)

关于java - 为什么会出现 java.lang.IllegalArgumentException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com