Java นิพจน์ทั่วไป
นิพจน์ทั่วไปคืออะไร?
นิพจน์ทั่วไปคือลำดับของอักขระที่สร้างรูปแบบการค้นหา เมื่อคุณค้นหาข้อมูลในข้อความ คุณสามารถใช้รูปแบบการค้นหานี้เพื่ออธิบายสิ่งที่คุณกำลังค้นหา
นิพจน์ทั่วไปอาจเป็นอักขระตัวเดียวหรือรูปแบบที่ซับซ้อนกว่าก็ได้
นิพจน์ทั่วไปสามารถใช้เพื่อดำเนินการค้นหาข้อความและ การแทนที่ข้อความ ทุกประเภท
Java ไม่มีคลาส Regular Expression ในตัว แต่เราสามารถนำเข้าjava.util.regex
แพ็คเกจเพื่อทำงานกับนิพจน์ทั่วไปได้ แพ็คเกจประกอบด้วยคลาสต่อไปนี้:
Pattern
คลาส - กำหนดรูปแบบ (เพื่อใช้ในการค้นหา)Matcher
คลาส - ใช้สำหรับค้นหารูปแบบPatternSyntaxException
คลาส - ระบุข้อผิดพลาดทางไวยากรณ์ในรูปแบบนิพจน์ทั่วไป
ตัวอย่าง
ค้นหาว่ามีคำว่า "w3schools" ในประโยคเกิดขึ้นหรือไม่:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Visit W3Schools!");
boolean matchFound = matcher.find();
if(matchFound) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}
// Outputs Match found
ตัวอย่างที่อธิบาย
ในตัวอย่างนี้ คำว่า "w3schools" กำลังถูกค้นหาในประโยค
ขั้นแรก รูปแบบจะถูกสร้างขึ้นโดยใช้Pattern.compile()
วิธีการ พารามิเตอร์แรกระบุว่ากำลังค้นหารูปแบบใด และพารามิเตอร์ที่สองมีแฟล็กเพื่อระบุว่าการค้นหาไม่ควรคำนึงถึงขนาดตัวพิมพ์ พารามิเตอร์ที่สองเป็นทางเลือก
เมธอด นี้matcher()
ใช้เพื่อค้นหารูปแบบในสตริง ส่งคืนวัตถุ Matcher ซึ่งมีข้อมูลเกี่ยวกับการค้นหาที่ดำเนินการ
เมธอดจะคืน ค่าfind()
จริง หากพบรูปแบบในสตริง และเป็นเท็จ หากไม่พบ
ธง
แฟล็กในcompile()
เมธอดจะเปลี่ยนวิธีดำเนินการค้นหา นี่คือบางส่วนของพวกเขา:
Pattern.CASE_INSENSITIVE
- กรณีของตัวอักษรจะถูกละเว้นเมื่อทำการค้นหาPattern.LITERAL
- อักขระพิเศษในรูปแบบจะไม่มีความหมายพิเศษใดๆ และจะถือเป็นอักขระธรรมดาเมื่อทำการค้นหาPattern.UNICODE_CASE
- ใช้ร่วมกับCASE_INSENSITIVE
ธงเพื่อละเว้นกรณีของตัวอักษรนอกตัวอักษรภาษาอังกฤษ
รูปแบบนิพจน์ทั่วไป
พารามิเตอร์แรกของPattern.compile()
วิธีการคือรูปแบบ มันอธิบายสิ่งที่กำลังค้นหา
วงเล็บถูกใช้เพื่อค้นหาช่วงของอักขระ:
Expression | Description |
---|---|
[abc] | Find one character from the options between the brackets |
[^abc] | Find one character NOT between the brackets |
[0-9] | Find one character from the range 0 to 9 |
เมตาคาแรคเตอร์
Metacharacters เป็นอักขระที่มีความหมายพิเศษ:
Metacharacter | Description |
---|---|
| | Find a match for any one of the patterns separated by | as in: cat|dog|fish |
. | Find just one instance of any character |
^ | Finds a match as the beginning of a string as in: ^Hello |
$ | Finds a match at the end of the string as in: World$ |
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
ปริมาณ
ปริมาณกำหนดปริมาณ:
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
n{x} | Matches any string that contains a sequence of X n's |
n{x,y} | Matches any string that contains a sequence of X to Y n's |
n{x,} | Matches any string that contains a sequence of at least X n's |
หมายเหตุ:หากนิพจน์ของคุณต้องการค้นหาอักขระพิเศษตัวใดตัวหนึ่ง คุณสามารถใช้แบ็กสแลช ( \ ) เพื่อหลีกหนีจากอักขระเหล่านั้น ใน Java แบ็กสแลชในสตริงจะต้องหลีกหนีจากตัวมันเอง ดังนั้น แบ็กสแลชสองตัวจึงจำเป็นในการหลีกเลี่ยงอักขระพิเศษ ตัวอย่างเช่น หากต้องการค้นหาเครื่องหมายคำถามตั้งแต่หนึ่งเครื่องหมายขึ้นไป คุณสามารถใช้นิพจน์ต่อไปนี้: "\\?"