Java RegEx no match found error

后端 未结 3 1843
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 04:35

Following regex giving me java.lang.IllegalStateException: No match found error

String requestpattern = \"^[A-Za-z]+ \\\\/+(\\\\w+)\";
Pattern p         


        
3条回答
  •  情深已故
    2020-12-10 04:56

    No match has been attempted. Call find() before calling 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));
    }
    

    Output:

    true
    upload
    

提交回复
热议问题