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

Matcher hasTransparentBounds() Method ใน Java พร้อมตัวอย่าง


คลาส java.util.regex.Matcher แทน engine ที่ดำเนินการจับคู่ต่างๆ ไม่มี constructor สำหรับคลาสนี้ คุณสามารถสร้าง/ได้รับ object ของคลาสนี้โดยใช้เมธอด matches() ของคลาส java.util.regex.Pattern

ใน regular expressions lookbehind และ lookahead constructs ใช้จับคู่ pattern เฉพาะที่มาก่อนหรือตามหลัง pattern อื่นๆ เช่น หากคุณต้องการยอมรับสตริงที่ยอมรับ 5 ถึง 12 ตัวอักษร regular expression จะเป็น −

"\\A(?=\\w{6,10}\\z)"

โดยค่าเริ่มต้น ขอบเขตของ matcher region ไม่โปร่งใสต่อ lookahead, lookbehind และ boundary matching กล่าวคือ constructs เหล่านี้ไม่สามารถจับคู่เนื้อหาของข้อความอินพุตนอกเหนือจากขอบเขต region ได้ −

เมธอด hasTransparentBounds() ของคลาสนี้ตรวจสอบว่ matcher ปัจจุบันใช้ transparent bounds หรือไม่ หากใช่ คืนค่า true มิฉะนั้น false

ตัวอย่างที่ 1: โดยไม่ตั้ง transparent bounds

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

public class HasTransparentBounds {
    public static void main(String[] args) {
        // Regular expression ยอมรับ 6 ถึง 10 ตัวอักษร
        String regex = "\\A(?=\\w{6,10}\\z)";
        System.out.println("Enter 5 to 12 characters: ");
        String input = new Scanner(System.in).next();
        
        // สร้าง Pattern object
        Pattern pattern = Pattern.compile(regex);
        
        // สร้าง Matcher object
        Matcher matcher = pattern.matcher(input);
        
        // ตั้ง region ให้กับสตริงอินพุต
        matcher.region(0, 4);
        
        if (matcher.find()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
        
        boolean bool = matcher.hasTransparentBounds();
        
        // สลับไปใช้ transparent bounds
        if (bool) {
            System.out.println("Current matcher uses transparent bounds");
        } else {
            System.out.println("Current matcher user non-transparent bound");
        }
    }
}

Output

Enter 5 to 12 characters: sampletext
Match not found
Current matcher user non-transparent bound

ตัวอย่างที่ 2: ตั้ง transparent bounds = true

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

public class HasTransparentBounds {
    public static void main(String[] args) {
        // Regular expression ยอมรับ 6 ถึง 10 ตัวอักษร
        String regex = "\\A(?=\\w{6,10}\\z)";
        System.out.println("Enter 5 to 12 characters: ");
        String input = new Scanner(System.in).next();
        
        // สร้าง Pattern object
        Pattern pattern = Pattern.compile(regex);
        
        // สร้าง Matcher object
        Matcher matcher = pattern.matcher(input);
        
        // ตั้ง region ให้กับสตริงอินพุต
        matcher.region(0, 4);
        matcher.useTransparentBounds(true);
        
        if (matcher.find()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
        
        boolean bool = matcher.hasTransparentBounds();
        
        // สลับไปใช้ transparent bounds
        if (bool) {
            System.out.println("Current matcher uses transparent bounds");
        } else {
            System.out.println("Current matcher user non-transparent bound");
        }
    }
}

Output

Enter 5 to 12 characters: sampletext
Match found
Current matcher uses transparent bounds