সাবএক্সপ্রেশন/মেটাক্যারেক্টার "\D" নন-ডিজিট ক্যারেক্টার ম্যাচ করে (অর্থাৎ [^0-9])।
উদাহরণ ১: নন-ডিজিট গণনা
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String args[]) {
String regex = "\\D";
String input = "This is sample text 12 24 56 89 24";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("ম্যাচের সংখ্যা: " + count);
}
}
আউটপুট
ম্যাচের সংখ্যা: 24
উদাহরণ ২: ৫টি স্ট্রিংয়ের নন-ডিজিট গণনা
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String args[]) {
String regex = "\\D";
Scanner sc = new Scanner(System.in);
System.out.println("৫টি ইনপুট স্ট্রিং দিন: ");
String input[] = new String[5];
for (int i = 0; i < 5; i++) {
input[i] = sc.nextLine();
}
Pattern p = Pattern.compile(regex);
System.out.println("প্রতিটি স্ট্রিং-এ নন-ডিজিট ক্যারেক্টার সংখ্যা: ");
for (int i = 0; i < 5; i++) {
Matcher m = p.matcher(input[i]);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("স্ট্রিং " + i + ": " + count);
}
}
}
আউটপুট
৫টি ইনপুট স্ট্রিং দিন: sample 1 12 35 36 63 test 243 663 hello hello how are you * প্রতিটি স্ট্রিং-এ নন-ডিজিট ক্যারেক্টার সংখ্যা: স্ট্রিং 0: 7 স্ট্রিং 1: 3 স্ট্রিং 2: 6 স্ট্রিং 3: 5 স্ট্রিং 4: 19
নোট: \\D স্পেস, পাঙ্কচুয়েশন, লেটার — সব নন-ডিজিট ম্যাচ করে।