【问题标题】:Getting Illegal State Exception in Regex在正则表达式中获取非法状态异常
【发布时间】:2019-05-18 13:45:52
【问题描述】:

我正在尝试使用正则表达式删除文本文件中的特定行,但我收到了非法状态异常。我最近试图习惯正则表达式并尝试使用 match.matches();但该解决方案对我不起作用。对我做错的任何建议

try {
    BufferedReader br = new BufferedReader(new FileReader("TestFile.txt"));
    //System.out.println(br.toString());

    ArrayList<String> list = new ArrayList<String>();
    String line= br.readLine() ;

    while (br.readLine() != null ) {
        //System.out.println(line);
        //System.out.println("test1"); { 
        Pattern regex = Pattern.compile("[^\\s\"]+|\"[^\"]*\"");
        Matcher regexMatcher = regex.matcher(line);
        String match = regexMatcher.group();// here is where the illegalstateexception occurs
        match = removeLeadingChar(match, "\"");
        match = removeLeadingChar(match, "\"");
        list.add(match);    
    //  }

    //      br.close();
    System.out.println(br);

线程“主”java.lang.IllegalStateException 中的异常:未找到匹配项 在 java.base/java.util.regex.Matcher.group(未知来源) 在 java.base/java.util.regex.Matcher.group(未知来源)

【问题讨论】:

  • 没有匹配结果。在使用group 方法之前,您应该调用find 方法来检查结果。 TestFile.txt 文件的内容是什么?
  • 我是正则表达式的新手,如果我使用 Matcher,find 方法的目的是什么?文本文件的内容是不需要的元数据2019-05-10T06:00: 00+00:00 pspapi-ctwbcp4a #Version: 1.0

标签: java illegalstateexception


【解决方案1】:

使用Matcher.find() 方法查看正则表达式模式是否匹配。在IDE(例如IntelliJ)中调试regexMatcher.find()方法的结果

try {
    BufferedReader br = new BufferedReader(new FileReader("TestFile.txt"));
    ArrayList<String> list = new ArrayList<>();

    String line;

    // Assign one line read from the file to a variable
    while ((line = br.readLine()) != null) {
        System.out.println(line);
        Pattern regex = Pattern.compile("[^\\s\"]+|\"[^\"]*\"");
        Matcher regexMatcher = regex.matcher(line);

        // Returns true if a match is found for the regular expression pattern.
        while (regexMatcher.find()) {
            String match = regexMatcher.group();
            match = removeLeadingChar(match, "\"");
            match = removeLeadingChar(match, "\"");
            list.add(match);
        }
    }

    // What is the purpose of this code?
    System.out.println(br);

    // If you want to output the string elements of the list
    System.out.println(list.toString());

    // must be closed after use.(to prevent memory leak)
    br.close();
} catch (IOException e) {
    // exception handling
    e.printStackTrace();
}

【讨论】:

    【解决方案2】:

    您的 while 循环错误,因此导致该行为空,请尝试:

    try {
        BufferedReader br = new BufferedReader(new FileReader("TestFile.txt"));
    
        ArrayList<String> list = new ArrayList<String>();
        String line;     // <--- FIXED
    
        while ((line = br.readLine()) != null) { // <--- FIXED
            Pattern regex = Pattern.compile("[^\\s\"]+|\"[^\"]*\"");
            Matcher regexMatcher = regex.matcher(line);
            String match = regexMatcher.group();// here is where the illegalstateexception occurs
            match = removeLeadingChar(match, "\"");
            match = removeLeadingChar(match, "\"");
            list.add(match);    
        }
    
        br.close();
    
        System.out.println(list.toString());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 2013-04-13
      • 2016-02-12
      • 2013-12-19
      相关资源
      最近更新 更多