Java?正則表達式高級用法實例詳解
Java 中的正則表達式(Regular Expressions)是強大的文本處理工具,可以用于搜索、匹配、替換和分割字符串。Java 提供了 java.util.regex 包來支持正則表達式的使用。以下是一些 Java 正則表達式的高級用法。
1. 使用 Pattern 和 Matcher
在 Java 中,正則表達式的使用通常涉及到 Pattern 和 Matcher 類。這兩個類使得正則表達式的編譯和匹配過程更為高效。
import java.util.regex.Pattern;
import java.util.regex.Matcher;
String input = "Hello, my email is example@example.com";
String regex = "\\w+@\\w+\\.com";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("找到的郵箱: " + matcher.group());
}2. 斷言(Assertions)
正則表達式中的斷言用于匹配某些條件但不包括這些條件的字符。常見的斷言有:
- 正向前瞻(Positive Lookahead)
(?=...) - 負向前瞻(Negative Lookahead)
(?!...) - 正向后顧(Positive Lookbehind)
(?<=...) - 負向后顧(Negative Lookbehind)
(?<!...)
示例:匹配包含數(shù)字的單詞,但不包含數(shù)字。
String input = "abc1 def2 ghi";
String regex = "\\b(?=\\w*)(?<!\\d)\\w+\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("匹配的單詞: " + matcher.group());
}3. 捕獲組和命名組
捕獲組用于提取匹配到的部分,命名組可以提高可讀性。
捕獲組
String input = "My name is John and I am 30 years old.";
String regex = "name is (\\w+) and I am (\\d+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
System.out.println("姓名: " + matcher.group(1)); // John
System.out.println("年齡: " + matcher.group(2)); // 30
}命名組
String input = "My name is John and I am 30 years old.";
String regex = "name is (?<name>\\w+) and I am (?<age>\\d+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
System.out.println("姓名: " + matcher.group("name")); // John
System.out.println("年齡: " + matcher.group("age")); // 30
}4. 替換操作
使用 Matcher 可以進行文本的替換。
String input = "The quick brown fox jumps over the lazy dog.";
String regex = "brown";
String replacement = "black";
String result = input.replaceAll(regex, replacement);
System.out.println("替換后的字符串: " + result); // The quick black fox jumps over the lazy dog.5. 分割字符串
使用正則表達式來分割字符串。
String input = "apple, banana; orange: melon";
String regex = "[,;:]\\s*"; // 逗號、分號、冒號及其后可選空格
String[] fruits = input.split(regex);
for (String fruit : fruits) {
System.out.println(fruit);
}6. 使用修飾符
可以使用修飾符來修改正則匹配的行為,例如:
Pattern.CASE_INSENSITIVE: 忽略大小寫Pattern.MULTILINE: 多行模式
例如:
String input = "Hello World\nhello world";
String regex = "hello";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("找到的匹配: " + matcher.group());
}7. Unicode 支持
Java 正則表達式支持 Unicode 字符,可以用于匹配國際化文本。
String input = "你好,世界";
String regex = "\\p{Han}+"; // 匹配漢字
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("找到的中文: " + matcher.group());
}到此這篇關于Java 正則表達式高級用法的文章就介紹到這了,更多相關Java 正則表達式用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot如何使用@ConfigurationProperties封裝配置文件
springboot如何使用@ConfigurationProperties封裝配置文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
玩轉spring boot 結合jQuery和AngularJs(3)
玩轉spring boot,這篇文章主要介紹了結合jQuery和AngularJs,玩轉spring boot,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
SpringBoot中GlobalExceptionHandler異常處理機制詳細說明
Spring Boot的GlobalExceptionHandler是一個全局異常處理器,用于捕獲和處理應用程序中發(fā)生的所有異常,這篇文章主要給大家介紹了關于Java中GlobalExceptionHandler異常處理機制的相關資料,需要的朋友可以參考下2024-03-03

