Simple Java regex matcher not working

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.


You must add this line before calling group():

m.find();

This moves the pointer to the start of the next match, if any - the method returns true if a match is found.

Usually, this is how you use it:

if (m.find()) {
    // access groups found. 
}