Java extract substring from url string
I want to extract substring from a url string. This is the url:
https://test.tech.com/public/pi?id=635106391297495358_0_280740c3f281419b954b309b45a41d77-M_M_0_56b6f628b90b4146abbdba1de9095657
I want to start extracting from id=
635106391297495358_0_280740c3f281419b954b309b45a41d77
until dash (-), and then extract the remaining substring
M_M_0_56b6f628b90b4146abbdba1de9095657
Note that the exact domain is not the one in the above, that's just an example .
Any ideas? I would gladly appreciate your help. Thanks.
UPDATE:
this is what I've done so far:
final URI uri = URI.create(result.getContents());
final String path = uri.getPath();
path.substring(path.lastIndexOf('-') + 1);
Log.e("EXTRACTED", "" + path);
But it just gets public/pi.
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
链接地址: http://www.djcxy.com/p/87018.html下一篇: Java从url字符串中提取子字符串