-4

I would like to extract all urls and "rel" informations associated in this string:

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

So I've started with:

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

It works perfectly for URL, but I don't know how to extract "rel" informations. In this example I want to extract "self" and "next".

Thank you very much guys

anthony
  • 7,653
  • 8
  • 49
  • 101
  • Not your down-voter, but if you're parsing HTML, why not use a dedicated HTML parser? Or if XML, same: why not use a dedicated XML parser? – Hovercraft Full Of Eels Nov 24 '17 at 13:37
  • 2
    Possible duplicate of [Regex Match all characters between two strings](https://stackoverflow.com/questions/6109882/regex-match-all-characters-between-two-strings) – BackSlash Nov 24 '17 at 13:41
  • Did you look at [the methods of Matcher](https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html)? – VGR Nov 24 '17 at 15:47

1 Answers1

1

You could do it this way:

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
}

This prints:

https://api-staging.xxx.com/v1/users
self
https://api-staging.xxx.com/v1/users?page=1,0
next
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76