Regular expression pattern for category support [duplicate]

First of all, uri.getPath() returns the path component, but what you're looking for is after the ?, so what you might want to try instead is uri.getQuery().

As for the matching:

Pattern p = Pattern.compile("id=(.+?)-");
Matcher m = p.matcher(uri.getQuery());
if (m.find()) {
    System.out.println(m.group(1));
}

Not tested, but I think it should work. The (.+?) is a capturing group that tries to match characters between the id= and the -.


One major problem is that:

 path.substring(path.lastIndexOf('-') + 1);

will not modify the variable path. The reason is that String are immutable and any change to them create a new string internally. If you want to get hold of the new substring reference then you need to assign it back to path:

 path = path.substring(path.lastIndexOf('-') + 1);

Now you can try more substring options


final URI uri = URI.create("https://test.tech.com/public/pi?id=635106391297495358_0_280740c3f281419b954b309b45a41d77-M_M_0_56b6f628b90b4146abbdba1de9095657");
    String queryString = uri.getQuery(); 
    String subString =  queryString.substring(queryString.lastIndexOf('-') + 1);
    System.out.println("EXTRACTED " + subString);

Produces:

EXTRACTED M_M_0_56b6f628b90b4146abbdba1de9095657