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

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


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

เมธอด reset() ของคลาส (Matcher) นี้ลบข้อมูลสถานะทั้งหมดและ reset character sequence เป็นค่าเริ่มต้น และ append position เป็นศูนย์

ตัวอย่างที่ 1: reset() ไม่มีอาร์กิวเมนต์

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

public class Reset {
    public static void main(String[] args) {
        String str = "<p>This <b>is</b> an <b>example</b>HTML <b>script</b> where <b>every</b> alternative <b>word</b> is <b>bold</b></p>.";
        // Regular expression to match contents of the bold tags
        String regex = "<b>(\\S+)</b>";
        
        // สร้าง Pattern object
        Pattern pattern = Pattern.compile(regex);
        
        // จับคู่ pattern ที่คอมไพล์แล้วใน String
        Matcher matcher = pattern.matcher(str);
        
        while (matcher.find()) {
            System.out.println("State of the matcher: " + matcher.toMatchResult());
            String result = matcher.group(1);
        }
        
        matcher = matcher.reset();
        System.out.println("State of the matcher after resetting it: " + matcher.toMatchResult());
    }
}

Output

State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>is</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>example</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>script</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>every</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>>word</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>bold</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=]

รูปแบบอื่นของเมธอดนี้รับข้อมูลสตริงและ reset Matcher ด้วยมัน

ตัวอย่างที่ 2: reset(String input)

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

public class Reset {
    public static void main(String[] args) {
        String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b> where <b>every</b> alternative <b>word</b> is <b>bold</b></p>.";
        // Regular expression to match contents of the bold tags
        String regex = "(\\S+)";
        
        // สร้าง Pattern object
        Pattern pattern = Pattern.compile(regex);
        
        // จับคู่ pattern ที่คอมไพล์แล้วใน String
        Matcher matcher = pattern.matcher(str);
        
        while (matcher.find()) {
            System.out.println("State of the matcher: " + matcher.toMatchResult());
            String result = matcher.group(1);
        }
        
        matcher = matcher.reset("<b>this</b> is <b>new</b> string <b>after</b> reset");
        while (matcher.find()) {
            System.out.println("State of the matcher after resetting it: " + matcher.toMatchResult());
        }
    }
}

Output

State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>is</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>example</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>script</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>every</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>word</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>bold</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>this</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>new</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>after</b>]