Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

Posix Character Class \p{Graph} ใน Java Regex

Character class \p{Graph} ใช้ match visible (graphical) characters คือ ตัวอักษรที่มองเห็นได้จริงบนหน้าจอ/กระดาษ ประกอบด้วย:

  • ตัวอักษร (Alphabets): a-z, A-Z และ Unicode letters
  • ตัวเลข (Digits): 0-9
  • Punctuation marks: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
  • Symbols: +, -, =, *, /, \, |, ~, @, #, $, %, ^, &, etc.

ไม่รวม: Whitespace (space, \t, \n, \r, \f, \v) และ Control characters

ความสัมพันธ์: \p{Graph} = \p{Print} - \p{Space}

ตัวอย่างที่ 1: นับจำนวน visible characters

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VisibleCharacters {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        String input = sc.nextLine();

        String regex = "\\p{Graph}";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        int count = 0;
        while (matcher.find()) {
            count++;
        }
        System.out.println("Number of visible characters: " + count);
    }
}

Output 1

Enter a string
öäü çã Ã test
Number of visible characters: 4

คำอธิบาย: เฉพาะ "test" ที่เป็น visible characters (space และ öäüçãà ไม่ถูกนับในบาง locale)

Output 2

Enter a string
welcome to tutorialspoint
Number of visible characters: 23

คำอธิบาย: "welcometutorialspoint" = 23 ตัวอักษร (ไม่นับ space 2 ช่อง)

ตัวอย่างที่ 2: หาสตริงที่ประกอบด้วย visible characters เท่านั้น

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        // Regex: เริ่มจบด้วย visible characters เท่านั้น (ไม่มี space)
        String regex = "^\\p{Graph}+$";

        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 visible characters:");
        for (int i = 0; i < 5; i++) {
            Matcher m = p.matcher(input[i]);
            if (m.matches()) {
                System.out.println(m.group());
            }
        }
    }
}

Output

Enter 5 input strings:
helloöäüçãÃ
öäüçãÃ
Hello1234
1234
welcome
Strings with only visible characters:
Hello1234
1234
welcome

ความแตกต่าง \p{Graph} vs \p{Print} vs \p{Alnum} vs \p{Punct}

Class รวม ตัวอย่าง Match ตัวอย่างไม่ Match
\p{Graph} Visible chars (Alnum + Punct) a, Z, 5, @, #, ไทย, 日 space, \t, \n
\p{Print} Graph + Space a, Z, 5, @, (space) \t, \n
\p{Alnum} Alphabetic + Numeric a, Z, 5, ไทย, 日 @, #, space
\p{Punct} Punctuation only @, #, !, ., , a, 5, space

กรณีใช้งาน

  • Validate password: ต้องมี visible chars เท่านั้น ไม่มี space: ^\\p{Graph}{8,}$
  • Parse tokens โดยไม่ต้องการ whitespace
  • Check ว่าสตริงมีเฉพาะตัวอักษรที่มองเห็นได้ (สำหรับ fixed-width display)