gpt4 book ai didi

java - 定位非法状态异常错误

转载 作者:行者123 更新时间:2023-12-02 05:39:37 26 4
gpt4 key购买 nike

我对java比较陌生,并且开发了一个程序,该程序使用多个正则表达式从网页中查找某些值。但是,有时我会遇到这样的错误:

 java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:468)
at java.util.regex.Matcher.group(Matcher.java:428)

我的代码包含很多正则表达式,我想知道 Eclipse 中是否有一种方法可以专门查找无法找到的模式。有没有办法找到哪一行代码产生了错误?提前致谢。

最佳答案

在从匹配器获取组之前进行检查,否则将导致以下异常:

java.lang.IllegalStateException: No match found

示例代码

if(matcher.find()){    // you can use while loop as well if there is more matches
matcher.group(1);
}
<小时/>

什么Matcher#group()方法抛出?

IllegalStateException if no match has yet been attempted, or if the previous match operation failed.

什么Matcher#find()状态:

Attempts to find the next subsequence of the input sequence that matches the pattern.

If the match succeeds then more information can be obtained via the start, end, and group methods.

关于java - 定位非法状态异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618385/

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