2014-03-03 40 views
1

我的程序每次运行时都会打印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); 
     } 
    } 
} 
+4

结果请不要只是手我们一串代码,并说:“请告诉我错了吗?”。 – 2014-03-03 00:32:37

+0

非法群组引用...这意味着您正试图访问不存在的内容。另外,为什么try/catch语句绝对没有?你应该能够在一定程度上处理这些事情。 – arco444

+0

@瑞克,我很抱歉,只是我无法弄清楚可能导致问题的原因。 – user2825125

回答

6

假说:在某处你的匹配组,存在形式的有效组参考"$n"其中n无法匹配原始Pattern中的任何组。

因此错误:“非法组参考”。

方案:使用"$2"而不将代替书写.group(2)

即:

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

写:

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

边注:没有必要逃避括号中的字符类; [^)]作品一样好[^\)],它更容易阅读;)

0

$java.lang.String.replaceFirst方法特殊字符。您需要将$转换为\\$

你可以从http://www.onstepr.com/posts/51

+0

尽管这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – thewaywewere

相关问题