यह कैरेक्टर क्लास \p{javaWhitespace} स्पेस से मेल खाती है। यह क्लास उन वर्णों से मेल खाती है जो java.lang.Character क्लास की isWhitespace() विधि को पैरामीटर के रूप में पास करने पर true वापस करते हैं।
उदाहरण 1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String args[]) {
// उपयोगकर्ता से स्ट्रिंग पढ़ना
System.out.println("Enter a string");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
// नियमित अभिव्यक्ति
String regex = "[\\p{javaWhitespace}]";
// नियमित अभिव्यक्ति संकलित करना
Pattern pattern = Pattern.compile(regex);
// मिलानकर्ता ऑब्जेक्ट प्राप्त करना
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
}
System.out.println("Number of whitespace characters: " + count);
}
}
आउटपुट
Enter a string: hello welcome to tutorialspoint Number of whitespace characters: 3
उदाहरण 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 = "^\\p{javaWhitespace}+$";
// इनपुट डेटा प्राप्त करना
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 input strings: ");
String input[] = new String[5];
for (int i = 0; i < 5; i++) {
input[i] = sc.nextLine();
}
// पैटर्न ऑब्जेक्ट बनाना
Pattern p = Pattern.compile(regex);
System.out.println("Strings with only whitespace characters: ");
for (int i = 0; i < 5; i++) {
// मिलानकर्ता ऑब्जेक्ट बनाना
Matcher m = p.matcher(input[i]);
if (m.matches()) {
System.out.println(m.group());
}
}
}
}
आउटपुट
Enter 5 input strings: Raju RAMU rahman radha SUnDar* Strings with only whitespace characters: RAMU