-2

在尝试将 matcher.group() 作为字符串获取时,它告诉它不可用。为什么?

在此处输入图像描述

代码:

private static ArrayList<String> validateList(List<String> listToProcess) {

        List<String> resultTemp = new ArrayList<String>();
        boolean listIsCorrectlyFormatted = true;


        String pattern = "";
        pattern = "(\\s)*(\\w\\w(\\w)*)((\\s)(\\w|-|\\.)+)?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+(\\+(\\d)+)+)?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+(\\w)+(\\w|\\s|-|,)*[^\\)|^\\(])?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+\\((\\w)+(\\w|\\s|-|,)*\\))?(\\s)*";
        // (\s)*(\w\w(\w)*)((\s)(\w|-|\.)+)?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+(\+(\d)+)+)?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+(\w)+(\w|\s|-|,)*[^\)|^\(])?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+\((\w)+(\w|\s|-|,)*\))?(\s)*


        String nameP = "(\\w\\w(\\w)*)((\\s)(\\w|-|\\.)+)?";
        String plussP = "(\\+(\\d)+)+";
        String commentP = "((\\w)+(\\w|\\s|-|,)*[^\\)|^\\(])|(\\((\\w)+(\\w|\\s|-|,)*\\))";
        String tmpStr = "";
        int counter = 1;
        Matcher matcher;

        Pattern generalPt = Pattern.compile(pattern);
        Pattern otherPattern = Pattern.compile(""); // *

        for (String str : listToProcess) {

            if (generalPt.matcher(str).find()) {
                // OK

                System.out.println("#"+counter+" :: OK ``"+str+"``");

                otherPattern = Pattern.compile(nameP); // name
                matcher = otherPattern.matcher(str);
                matcher.find();
                tmpStr +=  matcher.group(); // name
                System.out.println("@NAME@"+matcher.group()+"@");
                // -----------------------------------------------------
                matcher.reset();
                otherPattern = Pattern.compile(plussP); // plus
                matcher = otherPattern.matcher(str);
                matcher.find();

                 tmpMatcherGroup = matcher.group();
                tmpStr += tmpMatcherGroup;
                System.out.println("@PLUS@"+tmpMatcherGroup+"@");
                //--------------------------------------------------
                matcher.reset();
                otherPattern = Pattern.compile(commentP); // comment
                matcher = otherPattern.matcher(str);
                matcher.find();
                tmpStr +=  matcher.group();
                System.out.println("@COMMENT@"+matcher.group()+"@");
                //--------------------------------------------------
                resultTemp.add(tmpStr);

            } else {
                // NOK
                listIsCorrectlyFormatted = false;

                tmpStr = "ERROR at line: # " + counter;

                resultTemp.add(tmpStr);

                System.out.println("#"+counter+" :: NOT OK ``"+str+"``");
            }

            counter++;
        }

        List<String> result = new ArrayList<String>();
        result.add(Boolean.toString(listIsCorrectlyFormatted));
        result.addAll(resultTemp);

        return (ArrayList<String>) result;

    }

堆栈跟踪:

Thread [main] (Suspended (exception IllegalStateException)) 
    Matcher.group(int) line: not available  
    Matcher.group() line: not available [local variables unavailable]   
    DataProcess.validateList(List<String>) line: 111    
    DataProcess.main(String[]) line: 15 

解决方案:

matcher.reset();
                otherPattern = Pattern.compile(plussP); // plus
                matcher = otherPattern.matcher(str);
                matcher.find();
                try {
                    if (matcher.groupCount() > 0) {
                        tmpMatcherGroup = matcher.group();
                    }
                } catch (IllegalStateException e) {
                    System.out.println(e);
                }
                tmpStr += tmpMatcherGroup;
                System.out.println("@PLUS@" + tmpMatcherGroup + "@");
4

2 回答 2

1

您需要首先检查groupCount()如果它大于 0,那么您需要分配您的变量。

   while(m.find()) {
      for(int i = 0; i<=m.groupCount() ; i++) {
           ...Your code
      }
    }
于 2012-11-16T11:38:26.263 回答
1

解释在javadoc中:

抛出:

IllegalStateException - 如果尚未尝试匹配,或者之前的匹配操作失败

于 2012-11-16T11:39:59.803 回答