有一个用例,其中我有一个长的String
可以包含许多
我写了一个正则表达式(< code > "
如:
final String regex = "<img.*?\\\">";
final String string = "Hello World <img src=\"https://dummyimage.com/300.png/09f/777\"> \nMy Name <img src=\"https://dummyimage.com/300.png/09f/ff2\"> Random Text\nHello\nHello Random <img src=\"https://dummyimage.com/300.png/09f/888\"> \nMy Name <img src=\"https://dummyimage.com/300.png/09f/2ff\">adaad\n";
final String replace = "";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(replace); // Here, how can I collect all the image tags in a list
您可以简单地这样做:
final List<String> result = new ArrayList<>();
while (matcher.find()) {
result.add(matcher.group());
}
并摆脱最终的字符串替换 = “”;
我们可以使用所谓的Lookaheads和Lookbehinds拆分给定的字符串(有关更多信息,请查看下面提供的参考):
>
(?
<代码>(?
public static final Pattern ANGLE_BRACKETS =
Pattern.compile("(?<=.)(?=<)|(?<=>)(?=.)");
通过使用这种模式,我们在左尖括号和右尖括号边界的空字符串上生成一个子字符串流。然后过滤代表有效图像标签的字符串。
final String string = "Hello World <img src=\"https://dummyimage.com/300.png/09f/777\"> \nMy Name <img src=\"https://dummyimage.com/300.png/09f/ff2\"> Random Text\nHello\nHello Random <img src=\"https://dummyimage.com/300.png/09f/888\"> \nMy Name <img src=\"https://dummyimage.com/300.png/09f/2ff\">adaad\n";
List<String> imageTags = ANGLE_BRACKETS.splitAsStream(string)
.filter(str -> str.strip().matches("<img[^<]+>")) // verifying that a string is a valid image tag
.toList();
imageTags.forEach(System.out::println);
联机演示链接
在正则表达式中,你需要关心开头的尖括号
public static final Pattern IMG_TAG = Pattern.compile("img[^<]+>");
使用 Java 9 方法 Matcher.results(),
我们可以创建一个 MatchResult
对象流,其中包含有关给定字符串中捕获的序列的信息。为了获得匹配的子字符串,我们可以使用 MatchResult.group()。
final String string = "Hello World <img src=\"https://dummyimage.com/300.png/09f/777\"> \nMy Name <img src=\"https://dummyimage.com/300.png/09f/ff2\"> Random Text\nHello\nHello Random <img src=\"https://dummyimage.com/300.png/09f/888\"> \nMy Name <img src=\"https://dummyimage.com/300.png/09f/2ff\">adaad\n";
List<String> imageTags = IMG_TAG.matcher(string).results() // Stream<MatchResult>
.map(MatchResult::group) // Stream<String>
.toList();
imageTags.forEach(System.out::println);
输出:
<img src="https://dummyimage.com/300.png/09f/777">
<img src="https://dummyimage.com/300.png/09f/ff2">
<img src="https://dummyimage.com/300.png/09f/888">
<img src="https://dummyimage.com/300.png/09f/2ff">
联机演示链接