Java 如何从字符串中提取括号数据

Java 如何从字符串中提取括号数据,java,regex,string,matching,Java,Regex,String,Matching,我试图从下面的字符串中提取表示“rel=”next“”的链接。问题是四个的顺序可能会改变,这取决于是否存在指向“上一个”或“下一个”的链接。因此,我无法使用正则表达式或拆分为字符串数组并可靠地获取链接 下面是字符串: <http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,2015

我试图从下面的字符串中提取表示“rel=”next“”的链接。问题是四个的顺序可能会改变,这取决于是否存在指向“上一个”或“下一个”的链接。因此,我无法使用正则表达式或拆分为字符串数组并可靠地获取链接

下面是字符串:

<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=0&per_page=100>; rel="first",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=20&per_page=100>; rel="last",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next"
;rel=“第一次”,;rel=“last”,;rel=“下一步”
我需要得到这个字符串:

<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next"
;rel=“下一步”
以下是一个可读的版本:

<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=0&per_page=100>; rel="first",
<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=20&per_page=100>; rel="last",
<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next"
;rel=“first”,
; rel=“last”,
; rel=“下一步”
最终只提取API请求的链接。我尝试过按
拆分数组,但是URL可能包含
,因此这也是不可靠的。
谢谢

假设元素总是以
开头

演示:

您能澄清一下吗?您到底想做什么?我想您可以使用带有if的
find
,就像regex101上的演示中那样,所有内容都是逗号分隔的:
String[] elements = str.split(",(?=<http:)");
String myString = "<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=0&per_page=100>; rel=\"first\",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=20&per_page=100>; rel=\"last\",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel=\"next\"";
  try {
    Pattern regex = Pattern.compile("\"last\",(.*?)$");
    Matcher regexMatcher = regex.matcher(myString);
    if(regexMatcher.find()) {
        String next = regexMatcher.group(1);
        System.out.println(next);
    } 
   } catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
  }

//<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next"
"last",(.*?)$

Options: Case sensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Greedy quantifiers

Match the character string “"last",” literally (case sensitive) «"last",»
Match the regex below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is NOT a line break character (line feed) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of the string, or before the line break at the end of the string, if any (line feed) «$»