Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Matcher不';找不到模式_Java_Regex - Fatal编程技术网

Java 为什么Matcher不';找不到模式

Java 为什么Matcher不';找不到模式,java,regex,Java,Regex,我同意regex很简单,但我真的不明白为什么它不能找到和提取数据。另外,我对Java几乎没有经验,这可能是原因所在 方法1 String access_token = Utils.extractPattern(url, "access_token=([a-z0-9]+)&"); Url就像https://oauth.vk.com/blank.html#access_token=abcedefasdasdasdsadasasasdads123123&expires_in=0&user_i

我同意regex很简单,但我真的不明白为什么它不能找到和提取数据。另外,我对Java几乎没有经验,这可能是原因所在

方法1

String access_token = Utils.extractPattern(url, "access_token=([a-z0-9]+)&");
Url就像
https://oauth.vk.com/blank.html#access_token=abcedefasdasdasdsadasasasdads123123&expires_in=0&user_id=1111111111

Utils

public static String extractPattern(String string, String pattern) {
    Pattern searchPattern = Pattern.compile(pattern);
    Matcher matcher = searchPattern.matcher(string);
    Log.d("pattern found - ", matcher.matches() ? "yes" : "no");
    return matcher.group();
}
使用
java.lang.IllegalStateException失败的原因:到目前为止没有成功匹配

您需要使用
Matcher
类的
find()
方法来检查是否找到了
模式
。文件:

尝试查找输入序列的下一个子序列 与模式匹配

此方法从本节开头开始 matcher的区域,或者,如果以前调用过该方法 成功,并且匹配器从一开始就没有被重置 与上一个匹配项不匹配的字符

如果匹配成功,则可以通过 开始、结束和分组方法

以下内容应该有效:

public static String extractPattern(String string, String pattern) {
    Pattern searchPattern = Pattern.compile(pattern);
    Matcher matcher = searchPattern.matcher(string);
    if(matcher.find()){
        System.out.println("Pattern found");
        return matcher.group();
    }
    throw new IllegalArgumentException("Match not found");
}
您需要使用
Matcher
类的
find()
方法来检查是否找到了
模式。文件:

尝试查找输入序列的下一个子序列 与模式匹配

此方法从本节开头开始 matcher的区域,或者,如果以前调用过该方法 成功,并且匹配器从一开始就没有被重置 与上一个匹配项不匹配的字符

如果匹配成功,则可以通过 开始、结束和分组方法

以下内容应该有效:

public static String extractPattern(String string, String pattern) {
    Pattern searchPattern = Pattern.compile(pattern);
    Matcher matcher = searchPattern.matcher(string);
    if(matcher.find()){
        System.out.println("Pattern found");
        return matcher.group();
    }
    throw new IllegalArgumentException("Match not found");
}

只有当
matcher.matches()
时,才需要调用firstCall
matcher.group()
。旁注:您可能需要
matcher.group(1)
matcher.matches()
将不会像OP想要的那样匹配子字符串。改用
matcher.find()
。只有当
matcher.matches()
时,才需要调用firstCall
matcher.group()
。旁注:您可能需要
matcher.group(1)
matcher.matches()
将不会像OP想要的那样匹配子字符串。改用
matcher.find()
。谢谢你的帮助。希望进行测试,但远程服务似乎工作不稳定。顺便说一句,我应该阅读文档,但是Java上有很多文档!这在几个小时后的搜索中很有帮助,谢谢你的帮助。希望进行测试,但远程服务似乎工作不稳定。顺便说一句,我应该阅读文档,但是Java上有很多文档!这有助于在几个小时后搜索,谢谢