Question

i want regx to match any word of 2 or 1 characters example ( is , an , or , if, a )

i tried this :-

int scount = 0;
String txt = "hello everyone this is just test aa ";
Pattern p2 = Pattern.compile("\\w{1,2}");
Matcher m2 = p2.matcher(txt);

while (m2.find()) {
    scount++;
}

but got wrong matches.

Était-ce utile?

La solution

You probably want to use word boundary anchors:

Pattern p2 = Pattern.compile("\\b\\w{1,2}\\b");

These anchors match at the start/end of alphanumeric "words", that is, in positions before a \w character if there is no \w character before that, or after a \w character if there is no \w character after that.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top