सबएक्सप्रेशन/मेटाकैरेक्टर “\S” नॉन-व्हाइटस्पेस कैरेक्टर से मिलान करता है।
उदाहरण 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
String regex = "\\S";
String input = "Hello how are you welcome to Tutorialspoint !";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while(m.find()) {
count++;
}
System.out.println("मिलान की संख्या: " + count);
}
}
आउटपुट
मिलान की संख्या: 38
उदाहरण 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main( String args[] ) {
String regex = "\\S";
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 इनपुट स्ट्रिंग दर्ज करें: sample123 test data test *123 ab bc cd de hello how are you welcome to Tutorialspoint प्रत्येक स्ट्रिंग में कैरेक्टर की संख्या स्पेस के बिना: स्ट्रिंग 0: 9 स्ट्रिंग 1: 8 स्ट्रिंग 2: 8 स्ट्रिंग 3: 8 स्ट्रिंग 4: 37