কম্পিউটার টিউটোরিয়াল

জাভা রেগুলার এক্সপ্রেশনে \S মেটাক্যারেক্টার ব্যাখ্যা

সাবএক্সপ্রেশন/মেটাক্যারেক্টার "\S" নন-হোয়াইটস্পেস ক্যারেক্টার ম্যাচ করে (অর্থাৎ [^\\s])।

উদাহরণ ১: নন-স্পেস ক্যারেক্টার গণনা

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

উদাহরণ ২: ৫টি স্ট্রিংয়ের নন-স্পেস ক্যারেক্টার গণনা

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("৫টি ইনপুট স্ট্রিং দিন: ");
        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);
        }
    }
}

আউটপুট

৫টি ইনপুট স্ট্রিং দিন: 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
  1. জাভা রেগুলার এক্সপ্রেশনে \A মেটাক্যারেক্টার ব্যাখ্যা

  2. জাভা রেগুলার এক্সপ্রেশনে . (ডট) মেটাক্যারেক্টার ব্যাখ্যা

  3. জাভা রেগুলার এক্সপ্রেশনে $ (ডলার) মেটাক্যারেক্টার ব্যাখ্যা

  4. জাভা রেগুলার এক্সপ্রেশনে ^ (ক্যারেরট) মেটাক্যারেক্টার ব্যাখ্যা