目錄
- 一、正則表達式基礎
- (一)元字符
- (二)字符集
- (三)量詞
- 二、正則表達式常用示例
- (一)驗證郵箱格式
- (二)驗證電話號碼格式
- (三)提取網頁中的鏈接
- (四)驗證日期格式
- (五)驗證URL格式
- 三、正則表達式在Java中的應用
- (一)匹配操作
- (二)替換操作
- (三)分割操作
- 四、總結
一、正則表達式基礎
正則表達式是一種用于匹配字符串的強大工具。它使用特定的語法來定義匹配模式,可以在文本處理、表單驗證、數據提取等場景中發揮重要作用。
(一)元字符
元字符是正則表達式中的特殊字符,具有特殊含義。常見的元字符包括:
.
:匹配除換行符以外的任意單個字符。^
:匹配字符串的開始位置。$
:匹配字符串的結束位置。*
:匹配前面的子表達式零次或多次。+
:匹配前面的子表達式一次或多次。?
:匹配前面的子表達式零次或一次。{n}
:匹配前面的子表達式恰好 n 次。{n,m}
:匹配前面的子表達式至少 n 次,至多 m 次。[]
:定義一個字符集合,匹配其中的任意一個字符。|
:匹配左邊或右邊的表達式。()
:捕獲括號內的表達式,形成一個分組。
(二)字符集
字符集用于定義一組字符,匹配其中的任意一個字符。常見的字符集包括:
[abc]
:匹配 a、b 或 c。[a-z]
:匹配小寫字母 a 到 z。[A-Z]
:匹配大寫字母 A 到 Z。[0-9]
:匹配數字 0 到 9。[a-zA-Z0-9]
:匹配字母或數字。
(三)量詞
量詞用于指定前面的字符或子表達式出現的次數。常見的量詞包括:
*
:零次或多次。+
:一次或多次。?
:零次或一次。{n}
:恰好 n 次。{n,m}
:至少 n 次,至多 m 次。
二、正則表達式常用示例
(一)驗證郵箱格式
郵箱格式通常由本地部分、@ 符號和域名部分組成。本地部分可以包含字母、數字、下劃線、點和短橫線,域名部分可以包含字母、數字、點和短橫線。
public class EmailValidation {public static void main(String[] args) {String regex = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";String email1 = "test@example.com";String email2 = "invalid_email";System.out.println(email1.matches(regex)); // 輸出:trueSystem.out.println(email2.matches(regex)); // 輸出:false}
}
(二)驗證電話號碼格式
電話號碼格式可能因國家和地區而異。以下示例驗證一個簡單的電話號碼格式,例如:123-456-7890。
public class PhoneNumberValidation {public static void main(String[] args) {String regex = "^\\d{3}-\\d{3}-\\d{4}$";String phone1 = "123-456-7890";String phone2 = "1234567890";System.out.println(phone1.matches(regex)); // 輸出:trueSystem.out.println(phone2.matches(regex)); // 輸出:false}
}
(三)提取網頁中的鏈接
在網頁中,鏈接通常以 <a>
標簽的形式出現,href 屬性包含鏈接的 URL。
import java.util.regex.*;public class LinkExtractor {public static void main(String[] args) {String html = "<html><body><a href='https://example.com'>Example</a></body></html>";String regex = "href=['\"](.*?)['\"]";Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(html);while (matcher.find()) {System.out.println(matcher.group(1)); // 輸出:https://example.com}}
}
(四)驗證日期格式
日期格式通常為 YYYY-MM-DD 或 MM/DD/YYYY 等。以下示例驗證 YYYY-MM-DD 格式。
public class DateValidation {public static void main(String[] args) {String regex = "^\\d{4}-\\d{2}-\\d{2}$";String date1 = "2023-10-11";String date2 = "10/11/2023";System.out.println(date1.matches(regex)); // 輸出:trueSystem.out.println(date2.matches(regex)); // 輸出:false}
}
(五)驗證URL格式
URL 格式通常包括協議、域名和路徑等部分。以下示例驗證一個簡單的 URL 格式。
public class URLValidation {public static void main(String[] args) {String regex = "^(http|https)://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}(/[^\\s]*)?$";String url1 = "https://example.com";String url2 = "ftp://example.com";System.out.println(url1.matches(regex)); // 輸出:trueSystem.out.println(url2.matches(regex)); // 輸出:false}
}
三、正則表達式在Java中的應用
在 Java 中,可以使用 java.util.regex
包中的 Pattern
和 Matcher
類來處理正則表達式。
(一)匹配操作
import java.util.regex.*;public class RegexMatch {public static void main(String[] args) {String text = "Hello, World!";String regex = "World";Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(text);if (matcher.find()) {System.out.println("匹配成功!");} else {System.out.println("匹配失敗!");}}
}
(二)替換操作
import java.util.regex.*;public class RegexReplace {public static void main(String[] args) {String text = "Hello, World!";String regex = "World";String replacement = "Java";String result = text.replaceAll(regex, replacement);System.out.println(result); // 輸出:Hello, Java!}
}
(三)分割操作
import java.util.regex.*;public class RegexSplit {public static void main(String[] args) {String text = "apple,banana,cherry";String regex = ",";String[] result = text.split(regex);for (String s : result) {System.out.println(s);}// 輸出:// apple// banana// cherry}
}
四、總結
正則表達式是一種強大的文本處理工具,通過使用特定的語法可以定義復雜的匹配模式。在 Java 中,可以使用 Pattern
和 Matcher
類來處理正則表達式,實現匹配、替換和分割等操作。掌握正則表達式的基本語法和常用示例,可以大大提高我們在文本處理和數據驗證方面的開發效率。希望本文的講解和示例對您有所幫助,如果您在使用正則表達式時有任何疑問,歡迎隨時交流探討!