lundi 14 mai 2018

Multiple regex patterns to find. Java

I need to calculate separately number of words and sentences in string and i have these two methods which works fine:

    Pattern pattern = Pattern.compile("\\w+\\s|\\w+\\,|\\w+\\.|\\w+\\?|\\w+\\!*$");
    Matcher match1 = pattern.matcher(s);
    while(match1.find()) {
        counterWords++;
    }   

and for sentences:

    Pattern pattern = Pattern.compile("[^?!.][?!.]");
    Matcher match2 = pattern.matcher(s);
    while(match2.find()) {
        counterSentences++;
    }   

The next task is to calculate it again, but in one loop, so I tried:

while(match1.find() || match2.find()){
    if(match1.find()){
        counterWords++;
    }
    if(match2.find()){
        counterSentences++;
    }

However, the method is not working properly, it counts sentences correctly, but word counter is 2 times less that actual number of words. Most probably I don't understand completely how matcher.find() works, could somebody explain what I am doing wrong? Thank you.

Aucun commentaire:

Enregistrer un commentaire