使用拆分提取重叠匹配 [英] Extract overlapping matches using split
问题描述
如何使用 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屋!