2014-03-03 41 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

相關問題