Regex ใน Java คืออะไร?
นิพจน์ทั่วไป หรือ regex คือลำดับของอักขระที่สร้างรูปแบบ เมื่อคุณค้นหาข้อมูลใดๆ คุณสามารถใช้รูปแบบพิเศษนี้เพื่อค้นหาสตริงที่ตรงกัน อาจเป็นแบบเรียบง่ายเหมือนกับอักขระตัวเดียวหรืออาจเป็นรูปแบบที่ซับซ้อนมากขึ้นก็ได้ ใน Java คุณมีแพ็คเกจ Java regex ที่มีคลาสที่ให้ความสะดวกในการใช้นิพจน์ทั่วไปสำหรับการค้นหาและจัดการรูปแบบ แพ็คเกจนี้ถูกนำเข้าสู่โค้ดของคุณด้วยวิธีต่อไปนี้import java.util.regex.*;
Java Regex - ตัวจับคู่
import java.util.regex.Matcher;
แพ็คเกจ Java regex มีสามคลาส:
-
Pattern Class: เพื่อกำหนดรูปแบบที่ต้องการค้นหา
-
Matcher Class: เพื่อให้ตรงกับรูปแบบในข้อความและค้นหา
-
PatternSyntaxException Class: เพื่อระบุข้อผิดพลาดทางไวยากรณ์ในนิพจน์ทั่วไป
วิธีการคลาส Java Matcher
วิธีการ บางอย่างของ Java Matcher Class มีอธิบายไว้ด้านล่างนี้-
การจับคู่แบบบูล () — คืนค่าเป็นจริงหากนิพจน์ทั่วไปตรงกับรูปแบบ
-
boolean find() - ค้นหานิพจน์ทั่วไปถัดไปที่ตรงกับรูปแบบ
-
Boolean find(int start) - ค้นหานิพจน์ทั่วไปถัดไปที่ตรงกับรูปแบบจากจุดเริ่มต้นที่กำหนด
-
int start() - ส่งคืนดัชนีเริ่มต้นของการแข่งขัน
-
int end() - ส่งคืนดัชนีสิ้นสุดของการจับคู่ที่ส่งคืน
-
int groupCount() — คืนค่าจำนวนกลุ่มทั้งหมดในการแข่งขัน
-
สตริงแทนที่ทั้งหมด(การแทนที่สตริง) — แทนที่ทุกลำดับย่อยของการจับคู่ด้วยการแทนที่ที่กำหนด
-
สตริงreplaceFirst(การแทนที่สตริง) — แทนที่ลำดับย่อยแรกของการจับคู่ด้วยการแทนที่ที่กำหนด
ตัวอย่าง Java Matcher
มีวิธีการที่เป็นประโยชน์มากมายสำหรับ คลาส Matcherอย่างไรก็ตาม เราจะดูตัวอย่างเฉพาะของวิธีการ matchs() เพิ่มเติมโดยใช้เมธอดmatch()
ต่อไปนี้แสดงให้เห็นถึงตัวอย่าง Java Matcherสำหรับmatching()วิธีการimport java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
String regexPattern = "(.*)apple(.*)"; // this regex means that any char sequence can precede or succeed "apple"
String firstCheck = "Apples";
String secondCheck = "Apple";
String thirdCheck = "apple";
String fourthCheck = "An apple a day keeps the doctor away.";
String fifthCheck = "green apple, yellow apple, red apple, i love all kinds of apples.";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher1 = pattern.matcher(firstCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + firstCheck);
System.out.println("Output for matches(): " + matcher1.matches() + "\n");
Matcher matcher2 = pattern.matcher(secondCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + secondCheck);
System.out.println("Output for matches(): " + matcher2.matches() + "\n");
Matcher matcher3 = pattern.matcher(thirdCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + thirdCheck);
System.out.println("Output for matches(): " + matcher3.matches() + "\n");
Matcher matcher4 = pattern.matcher(thirdCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + fourthCheck);
System.out.println("Output for matches(): " + matcher4.matches() + "\n");
Matcher matcher5 = pattern.matcher(fifthCheck);
System.out.println("The Pattern is: " + pattern);
System.out.println("Text to check is: " + fifthCheck);
System.out.println("Output for matches(): " + matcher5.matches() + "\n");
}
}
เอาท์พุต
รูปแบบคือ: (.*)apple(.*) ข้อความที่จะตรวจสอบคือ: Apples Output formatchs(): false รูปแบบคือ: (.*)apple(.*) ข้อความที่จะตรวจสอบคือ: Apple Output formatchs() : false รูปแบบคือ: (.*)apple(.*) ข้อความที่จะตรวจสอบคือ: apple ผลลัพธ์สำหรับการจับคู่ (): true รูปแบบคือ: (.*)apple(.*) ข้อความที่จะตรวจสอบคือ: An apple a day ทำให้แพทย์ไม่อยู่ ผลลัพธ์สำหรับการจับคู่ (): true รูปแบบคือ: (.*)apple(.*) ข้อความที่จะตรวจสอบคือ: แอปเปิ้ลเขียว, แอปเปิ้ลเหลือง, แอปเปิ้ลแดง, ฉันชอบแอปเปิ้ลทุกชนิด เอาต์พุตสำหรับการจับคู่ (): จริง
GO TO FULL VERSION