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.
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);
}