使用拆分提取重叠匹配 [英] Extract overlapping matches using split

查看:79
本文介绍了使用拆分提取重叠匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 String.split()从输入中提取重叠匹配?

How can I extract overlapping matches from an input using String.split()?

例如,如果试图找到匹配aba

For example, if trying to find matches to "aba":

String input = "abababa";
String[] parts = input.split(???);

预期产出:

[aba, aba, aba]


推荐答案

String#split 不会给你重叠的匹配。因为字符串的特定部分只会包含在获得的数组的唯一索引中,而不包含在两个索引中。

String#split will not give you overlapping matches. Because a particular part of the string, will only be included in a unique index, of the array obtained, and not in two indices.

您应该使用模式匹配类。
您可以使用此正则表达式: -

You should use Pattern and Matcher classes here. You can use this regex: -

Pattern pattern = Pattern.compile("(?=(aba))");

并使用 Matcher #find 方法获取所有重叠的匹配,并打印 group(1)

And use Matcher#find method to get all the overlapping matches, and print group(1) for it.

上面的正则表达式匹配每个空字符串,然后是 aba ,然后打印第一个捕获的组。现在,因为预见零宽度断言,所以它不会消​​耗匹配的字符串。因此,您将获得所有重叠的匹配。

The above regex matches every empty string, that is followed by aba, then just print the 1st captured group. Now since look-ahead is zero-width assertion, so it will not consume the string that is matched. And hence you will get all the overlapping matches.

String input = "abababa";
String patternToFind = "aba";

Pattern pattern = Pattern.compile("(?=" + patternToFind + ")");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
    System.out.println(patternToFind + " found at index: " + matcher.start());
}

输出: -

aba found at index: 0
aba found at index: 2
aba found at index: 4

这篇关于使用拆分提取重叠匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆