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

জাভা রেজেক্সে POSIX ক্যারেক্টার ক্লাস \p{ASCII}

এই ক্লাস ASCII ক্যারেক্টারগুলো ম্যাচ করে, অর্থাৎ \x00-\x7F রেঞ্জ।

উদাহরণ

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("Enter a string");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        String regex = "^[\\p{ASCII}]";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        int count = 0;
        while(matcher.find()) {
            count++;
        }
        System.out.println("Number of non-ASCII characters in the given string: " + count);
    }
}

আউটপুট

Enter a string why do we fall
Number of non-ASCII characters in the given string: 1
  1. জাভা রেজেক্সে POSIX ক্যারেক্টার ক্লাস \p{Upper}

  2. জাভা রেজেক্সে POSIX ক্যারেক্টার ক্লাস \p{Lower}

  3. জাভা রেগেক্স দিয়ে নন-ওয়ার্ড ক্যারেক্টার ম্যাচ করা (\W)

  4. জাভা রেগেক্স দিয়ে যেকোনো ক্যারেক্টার ম্যাচ করা (.)