Matcher not finding overlapping words?

前端 未结 4 456
猫巷女王i
猫巷女王i 2021-01-28 08:05

I\'m trying to take a string:

String s = \"This is a String!\";

And return all 2-word pairs within that string. Namely:

{\"this         


        
4条回答
  •  广开言路
    2021-01-28 08:33

    I like the two answers already posted, counting words and subtracting one, but if you just need a regex to find overlapping matches:

    Pattern pattern = Pattern.compile('\\S+ \\S+');
    Matcher matcher = pattern.matcher(inputString);
    int matchCount = 0;
    boolean found = matcher.find();
    while (found) {
      matchCount += 1;
      // search starting after the last match began
      found = matcher.find(matcher.start() + 1);
    }
    

    In reality, you'll need to be a little more clever than simply adding 1, since trying this on "the force" will match "he force" and then "e force". Of course, this is overkill for counting words, but this may prove useful if the regex is more complicated than that.

提交回复
热议问题