gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:27:44 25 4
gpt4 key购买 nike

我正在尝试从下面的字符串中提取“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"

我需要获取这个字符串:

<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"

这是一个可读版本:

<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"

最终仅提取 API 请求的链接。我尝试按 , 拆分数组,但是 URL 可能包含 ,,因此这也是不可靠的。谢谢!

最佳答案

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) «$»
<小时/>

演示: http://ideone.com/7mITYJ

关于java - 如何从字符串中提取括号数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36584028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com