gpt4 book ai didi

Java RegEx 找不到匹配错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:27:38 26 4
gpt4 key购买 nike

下面的正则表达式给我 java.lang.IllegalStateException: No match found 错误

String requestpattern = "^[A-Za-z]+ \\/+(\\w+)";
Pattern p = Pattern.compile(requestpattern);
Matcher matcher = p.matcher(requeststring);
return matcher.group(1);

请求字符串在哪里

POST //upload/sendData.htm HTTP/1.1

如有任何帮助,我们将不胜感激。

最佳答案

未尝试匹配。在调用 group() 之前调用 find()

public static void main(String[] args) {
String requeststring = "POST //upload/sendData.htm HTTP/1.1";
String requestpattern = "^[A-Za-z]+ \\/+(\\w+)";
Pattern p = Pattern.compile(requestpattern);
Matcher matcher = p.matcher(requeststring);
System.out.println(matcher.find());
System.out.println(matcher.group(1));
}

输出:

true
upload

关于Java RegEx 找不到匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16344088/

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