Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any regular expression to return the number of matches

I’m trying to return how many matches I’ve got using regex on a single text. I need the regex to be global and case insensitive so it can match what I need.

What I actually need is the following:

  • Given this regex String regex = "one[^\w]*million" (global and case-insensitive)
  • Given this text as input String text = "estoOne␣⏎␣Million jvegax ONE␣MILLION million one OnE>>% mIllion spam"
  • It should return 3 matches on the string count = 3

What I actually have is just a simple check that returns true or false if at least one of the character matches the sequence, but not the actual number of matches.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(final String[] args) {
        Main.findStrongSpamWords();
    }

    public static void findStrongSpamWords() {
        final String body = "estoOne␣⏎␣Million jvegax ONE␣MILLION million one OnE>>% mIllion spam";
        final Pattern pattern = Pattern.compile("one[^\\w]*million", Pattern.CASE_INSENSITIVE);
        final Matcher matchResult = pattern.matcher(body);
        
        System.out.println(matchResult.find());
    }
}

>Solution :

There are no regex that can do that. However, with Java 9 and later, use Matcher.results().count().

    public static void findStrongSpamWords() {
        final String body = "estoOne␣⏎␣Million jvegax ONE␣MILLION million one OnE>>% mIllion spam";
        final Pattern pattern = Pattern.compile("one[^\\w]*million", Pattern.CASE_INSENSITIVE);
        final Matcher matchResult = pattern.matcher(body);
        
        System.out.println(matchResult.results().count());
    }

If you are on Java 8 or earlier, you must count yourself:

public static void findStrongSpamWords() {
    final String body = "estoOne␣⏎␣Million jvegax ONE␣MILLION million one OnE>>% mIllion spam";
    final Pattern pattern = Pattern.compile("one[^\\w]*million", Pattern.CASE_INSENSITIVE);
    final Matcher matchResult = pattern.matcher(body);
    
    int count = 0;
    while (matchResult.find()) {
      count++;
    }
    System.out.println(count);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading