Simple Java regex matcher not working

后端 未结 2 1913
名媛妹妹
名媛妹妹 2020-12-01 19:25

Code :

import java.util.regex.*;

public class eq {
    public static void main(String []args) {
        String str1 = \"some=String&Here&modelId=324         


        
2条回答
  •  我在风中等你
    2020-12-01 20:18

    You need to call find() on the Matcher before you can call group() and related functions that queries about the matched text or manipulate it (start(), end(), appendReplacement(StringBuffer sb, String replacement), etc.).

    So in your case:

    if (m.find()) {
        System.out.println("id = " + m.group(1));
    }
    

    This will find the first match (if any) and extract the first capturing group matched by the regex. Change if to while loop if you want to find all matches in the input string.

提交回复
热议问题