Java:如何提取信息" rel =" self""与正则表达式

时间:2017-11-24 13:33:36

标签: java regex

我想提取所有网址和" rel"此字符串中的相关信息:

<https://api-staging.xxx.com/v1/users>; rel="self", <https://api-staging.xxx.com/v1/users?page=1,0>; rel="next"

所以我开始:

Pattern mentionPattern = Pattern.compile("<(.+?)>");
Matcher mentionMatcher = mentionPattern.matcher(url);

它适用于网址,但我不知道如何提取&#34; rel&#34;信息。在这个例子中,我想提取&#34; self&#34;和&#34;下一个&#34;。

非常感谢你们

1 个答案:

答案 0 :(得分:1)

你可以这样做:

String test = "<https://api-staging.xxx.com/v1/users>; rel=\"self\", <https://api-staging.xxx.com/v1/users?page=1,0>; rel=\"next\"";
Pattern mentionPattern = Pattern.compile("[<\"](?<content>.+?)[>\"]");
Matcher m = mentionPattern.matcher(test);
while(m.find()) {
    System.out.println(m.group("content")); // using named groups
}

打印:

https://api-staging.xxx.com/v1/users
self
https://api-staging.xxx.com/v1/users?page=1,0
next