I\'m trying to take a string:
String s = \"This is a String!\";
And return all 2-word pairs within that string. Namely:
{\"this
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.