如何在 java 中获取<>之间的字符串



我有一个网址链接

Link=<http://mysample.link.com/first/12/sample?page=1234-asdf>;rel="next"

我想获取<>之间的所有内容,并将结果作为http://mysample.link.com/first/12/sample?page=1234-asdf

目前我正在使用 String.substring() 像 finalString=sample.subString(sample.indexOf("<"),sample.indexOf(">"));

但我不认为这是最好的方法。有人可以告诉我如何使用正则表达式获取结果字符串。

这是一个正则表达式:

<([^>]+)>

示例:http://regex101.com/r/lT3xS4

我会使用这个:(?<=Link=<).+?(?=>)只捕获链接"标签"中的实际网址。

以下内容将为您工作。

String s = "Link=<http://mysample.link.com/first/12/sample?page=1234-asdf>;rel="next"";
Pattern p = Pattern.compile("<([^>]+)>");
Matcher m = p.matcher(s);
while (m.find()) {
  System.out.println(m.group(1));
}
// "http://mysample.link.com/first/12/sample?page=1234-asdf"

最新更新