सबएक्सप्रेशन/मेटाकैरेक्टर “\D” नॉन-डिजिट से मिलान करता है।
उदाहरण 1
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
उदाहरण 2
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("5 इनपुट स्ट्रिंग दर्ज करें: ");
String input[] = new String[5];
for (int i=0; i<5; i++) {
input[i] = sc.nextLine();
}
// Pattern ऑब्जेक्ट बनाना
Pattern p = Pattern.compile(regex);
System.out.println("प्रत्येक स्ट्रिंग में नॉन-डिजिट कैरेक्टर की संख्या: ");
for(int i=0; i<5;i++) {
// Matcher ऑब्जेक्ट बनाना
Matcher m = p.matcher(input[i]);
int count = 0;
while(m.find()) {
count++;
}
System.out.println("स्ट्रिंग " + i + ": " + count);
}
}
}
आउटपुट
5 इनपुट स्ट्रिंग दर्ज करें: sample 1 12 35 36 63 test 243 663 hello hello how are you * प्रत्येक स्ट्रिंग में नॉन-डिजिट कैरेक्टर की संख्या: स्ट्रिंग 0: 7 स्ट्रिंग 1: 3 स्ट्रिंग 2: 6 स्ट्रिंग 3: 5 स्ट्रिंग 4: 19