อักขระเมตา "." จับคู่อักขระทั้งหมด เพื่อพิมพ์อักขระทั้งหมดโดยใช้นิพจน์ทั่วไป -
-
รวบรวมนิพจน์ทั่วไปโดยใช้วิธีการคอมไพล์ ()
-
สร้างวัตถุ Matcher โดยใช้เมธอด matcher()
-
ค้นหารายการที่ตรงกันโดยใช้เมธอด find() และพิมพ์เนื้อหาที่ตรงกัน (ตัวอักษร) ที่ตรงกันทุกรายการโดยใช้เมธอด group()
ตัวอย่าง
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
//Regular expression to match a string of non-word with length 2 to 6
String regex = ".";
Scanner sc = new Scanner(System.in);
System.out.println("Enter your input string: ");
String input = sc.nextLine();
//Creating a Pattern object
Pattern p = Pattern.compile(regex);
//Creating a Matcher object
Matcher m = p.matcher(input);
while(m.find()) {
System.out.println(m.group());
}
}
} ผลลัพธ์
Enter your input string: This is a sample text T h i s i s a s a m p l e t e x t