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

วิธีจับคู่จุดเริ่มต้นของสตริง/บรรทัดเฉพาะด้วย Java RegEx

เมตาอักขระ “^” จับคู่จุดเริ่มต้นของสตริงเฉพาะ นั่นคือ จับคู่ตัวอักษรแรกของสตริง เช่น

  • นิพจน์ “^\\d” จับคู่สตริง/บรรทัดที่เริ่มต้นด้วยตัวเลข

  • นิพจน์ “^[a-z]” จับคู่สตริง/บรรทัดที่เริ่มต้นด้วยตัวอักษรพิมพ์เล็ก

ตัวอย่างที่ 1

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 = "^[^a-zA-Z0-9//s].*"; 
        // คอมไพล์นิพจน์ปกติ 
        Pattern pattern = Pattern.compile(regex); 
        // ดึงออบเจ็กต์ matcher 
        Matcher matcher = pattern.matcher(input); 
        if(matcher.matches()) { 
            System.out.println("Match occurred"); 
        } else { 
            System.out.println("Match not occurred"); 
        } 
    } 
}

ผลลัพธ์

Enter a String #starting with a special character 
Match occurred

ตัวอย่างที่ 2

import java.util.Scanner; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
public class RegexExample { 
    public static void main( String args[] ) { 
        String regex = "\\.$"; 
        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 
        Pattern p = Pattern.compile(regex); 
        for(int i=0; i<5;i++) { 
            // สร้างออบเจ็กต์ Matcher 
            Matcher m = p.matcher(input[i]); 
            if(m.find()) { 
                System.out.println("String " + i + " ends with '.'"); 
            } 
        } 
    } 
}

ผลลัพธ์

Enter 5 input strings: hello how are you. where do you live what is your name. welcome to tutorialspoint The Biggest Online Tutorials Library. 
String 0 ends with '.' 
String 2 ends with '.' 
String 4 ends with '.' 

ตัวอย่างที่ 3

import java.util.Scanner; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
public class RegexExample { 
    public static void main( String args[] ) { 
        String regex = "^[A-Z]"; 
        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); 
        for(int i=0; i<5;i++) { 
            Matcher m = p.matcher(input[i]); 
            if(m.find()) { 
                System.out.println("String " + i + " starts with a capital letter"); 
            } 
        } 
    } 
}

ผลลัพธ์

Enter 5 input strings: Sample text1 sample text2 hello how are you Welcome to tutorialspoint Good morning 
String 0 starts with a capital letter 
String 3 starts with a capital letter 
String 4 starts with a capital letter