Java RegEx找不到匹配错误

Ana*_*nda 8 java regex

正则表达式给我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);
Run Code Online (Sandbox Code Playgroud)

请求字符串在哪里

POST //upload/sendData.htm HTTP/1.1
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

acd*_*ior 29

没有尝试匹配.打电话find()前打电话group().

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));
}
Run Code Online (Sandbox Code Playgroud)

输出:

true
upload
Run Code Online (Sandbox Code Playgroud)