मेटाकैरेक्टर "\\s" दिए गए स्ट्रिंग में व्हाइटस्पेस कैरेक्टर से मिलान करता है।
उदाहरण 1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String args[]) {
// उपयोगकर्ता से स्ट्रिंग पढ़ना
System.out.println("एक स्ट्रिंग दर्ज करें");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String regex = "\\s";
// रेगुलर एक्सप्रेशन संकलित करना
Pattern pattern = Pattern.compile(regex);
// Matcher ऑब्जेक्ट प्राप्त करना
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
}
System.out.println("स्पेस की संख्या: " + count);
}
}
आउटपुट
एक स्ट्रिंग दर्ज करें Hello how are you welcome to tutorialspoint स्पेस की संख्या: 6
उदाहरण 2
import java.util.Scanner;
public class RegexExample {
public static void main( String args[] ) {
// रेगुलर एक्सप्रेशन
String regex = "\\s+";
System.out.println("इनपुट वैल्यू दर्ज करें: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String result = input.replaceAll(regex, "");
System.out.println("परिणाम: " + result);
}
}
आउटपुट
इनपुट वैल्यू दर्ज करें: hello how are you परिणाम: hellohowareyou