在Java中查找字符串中出现的所有子字符串

时间:2021-05-25 19:20:23

I'm trying to find all occurrences of a substring in a string in Java.

我试图在Java中查找字符串中所有出现的子字符串。

For example: searching "ababsdfasdfhelloasdf" for "asdf" would return [8,17] since there are 2 "asdf"'s, one at position 8 and one at 17. Searching "aaaaaa" for "aa" would return [0,1,2,3,4] because there is an "aa" at positions 0,1,2,3, and 4.

例如:搜索“ababsdfasdfhelloasdf”为“asdf”将返回[8,17],因为有2个“asdf”,一个位于8位,一个位于17位。搜索“aaaaaa”中的“aa”将返回[0,1, 1,2,3,4]因为在位置0,1,2,3和4处有一个“aa”。

I tried this:

我试过这个:

public List<Integer> findSubstrings(String inwords, String inword) {
    String copyOfWords = inwords;
    List<Integer> indicesOfWord = new ArrayList<Integer>();
    int currentStartIndex = niwords.indexOf(inword);
    int indexat = 0;
    System.out.println(currentStartIndex);
    while (cthing1 > 0) {
        indicesOfWord.add(currentStartIndex+indexat);
        System.out.println(currentStartIndex);
        System.out.println(indicesOfWord);
        indexat += cthing1;
        copyOfWords = copyOfWords.substring(cthing1);
        System.out.println(copyOfWords);
        cthing1 = copyOfWords.indexOf(inword);
    }

This problem can be solved in Python as follows:

这个问题可以在Python中解决如下:

indices = [m.start() for m in re.finditer(word, a.lower())]

where "word" is the word I'm looking for and "a" is the string I'm searching through.

“word”是我正在寻找的单词,“a”是我正在搜索的字符串。

How can I achieve this in Java?

我怎样才能在Java中实现这一目标?

2 个解决方案

#1


6  

You can use capturing inside a positive look-ahead to get all overlapping matches and use Matcher#start to get the indices of the captured substrings.

您可以在正向前瞻中使用捕获来获取所有重叠匹配,并使用Matcher #start来获取捕获的子串的索引。

As for the regex, it will look like

至于正则表达式,它看起来像

(?=(aa))

In Java code:

在Java代码中:

String s = "aaaaaa";
Matcher m = Pattern.compile("(?=(aa))").matcher(s);
List<Integer> pos = new ArrayList<Integer>();
while (m.find())
{
    pos.add(m.start());
}
System.out.println(pos);

Result:

结果:

[0, 1, 2, 3, 4]

See IDEONE demo

请参阅IDEONE演示

#2


0  

Using a regex is definitely an overly heavy solution for finding substrings, and it'll especially be a problem if your substring contains special regex characters like .. Here's a solution adapted from this answer:

使用正则表达式对于查找子字符串来说绝对是一个过于繁重的解决方案,如果你的子字符串包含特殊的正则表达式字符,那么它将特别成问题。这是一个改编自这个答案的解决方案:

String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
List<Integer> result = new ArrayList<Integer>();

while(lastIndex != -1) {

    lastIndex = str.indexOf(findStr,lastIndex);

    if(lastIndex != -1){
        result.add(lastIndex);
        lastIndex += 1;
    }
}

#1


6  

You can use capturing inside a positive look-ahead to get all overlapping matches and use Matcher#start to get the indices of the captured substrings.

您可以在正向前瞻中使用捕获来获取所有重叠匹配,并使用Matcher #start来获取捕获的子串的索引。

As for the regex, it will look like

至于正则表达式,它看起来像

(?=(aa))

In Java code:

在Java代码中:

String s = "aaaaaa";
Matcher m = Pattern.compile("(?=(aa))").matcher(s);
List<Integer> pos = new ArrayList<Integer>();
while (m.find())
{
    pos.add(m.start());
}
System.out.println(pos);

Result:

结果:

[0, 1, 2, 3, 4]

See IDEONE demo

请参阅IDEONE演示

#2


0  

Using a regex is definitely an overly heavy solution for finding substrings, and it'll especially be a problem if your substring contains special regex characters like .. Here's a solution adapted from this answer:

使用正则表达式对于查找子字符串来说绝对是一个过于繁重的解决方案,如果你的子字符串包含特殊的正则表达式字符,那么它将特别成问题。这是一个改编自这个答案的解决方案:

String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
List<Integer> result = new ArrayList<Integer>();

while(lastIndex != -1) {

    lastIndex = str.indexOf(findStr,lastIndex);

    if(lastIndex != -1){
        result.add(lastIndex);
        lastIndex += 1;
    }
}