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

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

Character class \p{Alnum} ใช้ match alphanumeric characters คือ ตัวอักษรและตัวเลข รวมถึง:

  • ตัวอักษรภาษาอังกฤษ: a-z, A-Z
  • ตัวเลข: 0-9
  • ตัวอักษร Unicode อื่นๆ ที่เป็น Letter หรือ Number (เช่น ไทย, จีน, ฮิรางานา, คิริลลิก, อรบิก)

เทียบเท่ากับ [a-zA-Z0-9] สำหรับ ASCII แต่ \p{Alnum} กว้างกว่าครอบคลุม Unicode

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

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

public class AlphanumericExample {
    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{Alnum}";

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

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

Output 1

Enter a string
sample 123
Number of alphanumeric characters: 9

คำอธิบาย: s,a,m,p,l,e,1,2,3 = 9 ตัว (space ไม่นับ)

Output 2

Enter a string
@$#%&&#*
Number of alphanumeric characters: 0

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

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

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

        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 alphanumeric 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
1234
243test
##$$@
***
Strings with only alphanumeric characters:
hello
1234
243test

ความแตกต่าง \p{Alnum} vs \p{Alpha} vs \p{Digit} vs \p{XDigit}

Class Match เทียบเท่า (ASCII)
\p{Alnum} Letters + Digits [a-zA-Z0-9]
\p{Alpha} Letters only [a-zA-Z]
\p{Digit} Digits only (0-9) [0-9] = \d
\p{XDigit} Hex digits [0-9a-fA-F]

กรณีใช้งานทั่วไป

  • Validate username: ^\\p{Alnum}{3,20}$ (3-20 ตัว alphanumeric)
  • Validate alphanumeric ID: ^\\p{Alnum}{10}$
  • Remove non-alphanumeric: str.replaceAll("[^\\p{Alnum}]", "")
  • Check password strength: ต้องมีทั้ง letter และ digit
  • Sanitize input สำหรับ database keys, filenames

หมายเหตุ

  • \p{Alnum} ใช้ Pattern.UNICODE_CHARACTER_CLASS โดย default ใน Java 7+
  • สำหรับ ASCII-only ใช้ [a-zA-Z0-9] จะเร็วกว่าและชัดเจนกว่า
  • Thai characters (ก-ฮ, ะ-๙) ถูกจัดเป็น Alnum เพราะเป็น Unicode Letters