I think that you should be a bit more descriptive. Your current code returns 15 from the variable scount
. That's not nothing.
If you want to get a count of the 2 letter words, and that is excluding underscores, digits within this count, I think that you would be better off with negative lookarounds:
Pattern.compile("(?i)(?<![a-z])[a-z]{1,2}(?![a-z])");
With a string input of hello everyone this is just 1 test aa
, you get the value of scount
as 2 (is
and aa
) and not 3 (is
, 1
, aa
) as you would have if you were looking for only 1 or 2 consecutive \w
.
Also, with hello everyone this is just test aa_
, you get a count of 1 with \w
(is
), but 2 (is
, aa
)with the lookarounds.