[英]Java extract substring from url string
I want to extract substring from a url string. 我想从url字符串中提取子字符串。 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= 我想从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. 但它只是公开/ 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()
. 首先,
uri.getPath()
返回路径组件,但您要查找的是?,所以您可能想要尝试的是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 -
. (.+?)
是一个捕获组,它尝试匹配id=
和-
之间的字符。
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.
原因是String是不可变的,对它们的任何更改都会在内部创建一个新字符串。 If you want to get hold of the new substring reference then you need to assign it back to
path
: 如果要获取新的子字符串引用,则需要将其分配回
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
提取M_M_0_56b6f628b90b4146abbdba1de9095657
Here is the solution, which I hope is self explanatory: 这是解决方案,我希望是自解释的:
public static void main(String[] argv) {
final String uriStr = "https://test.tech.com/public/pi?id=635106391297495358_0_280740c3f281419b954b309b45a41d77-M_M_0_56b6f628b90b4146abbdba1de9095657";
final URI uri = URI.create(uriStr);
final String query = uri.getQuery();
System.out.println(String.format("EXTRACTED QUERY [%s]", query));
final String part1 = query.substring(query.indexOf('=')+1, query.indexOf('-'));
System.out.println(String.format("EXTRACTED PART 1 [%s]", part1));
final String part2 = query.substring(query.indexOf('-')+1);
System.out.println(String.format("EXTRACTED PART 2 [%s]", part2));
}
}* } *
Here is the output: EXTRACTED QUERY [id=635106391297495358_0_280740c3f281419b954b309b45a41d77-M_M_0_56b6f628b90b4146abbdba1de9095657] 这是输出:提取的查询[id = 635106391297495358_0_280740c3f281419b954b309b45a41d77-M_M_0_56b6f628b90b4146abbdba1de9095657]
EXTRACTED PART 1 [635106391297495358_0_280740c3f281419b954b309b45a41d77] 摘录第1部分[635106391297495358_0_280740c3f281419b954b309b45a41d77]
EXTRACTED PART 2 [M_M_0_56b6f628b90b4146abbdba1de9095657] 摘录第2部分[M_M_0_56b6f628b90b4146abbdba1de9095657]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.