智慧產生和解釋正規表示式,支援各種常見模式比對
將您的文字指令轉換為公式,或輸入公式獲取解釋。
透過和 AI 對話來線上編輯 Excel
使用 AI 將您的文字指令轉換為 SQL 查詢語句。
產生 Excel VBA (Visual Basic for Applications) 程式碼,用於自動化任務和建立 Microsoft Excel 中的自訂解決方案。
上傳您的 Excel 檔案,使用我們的 AI 驅動的圖表產生器產生漂亮的圖表。
使用我們的 AI 驅動的心智圖產生器將您的文字轉換為漂亮的心智圖。輕鬆編輯和自訂您的心智圖。
使用 AI 智慧產生和解釋正規表示式,支援各種文字模式比對和資料驗證。
使用AI根據文字描述生成精美圖片,支援多種尺寸,免費
Java 正则表达式(Regex)是文本处理的强大工具,广泛用于字符串匹配、验证、替换等操作。本文为你呈现最实用的 Java 正则表达式教程,附带详细代码示例,助你掌握 Java 正则技能,轻松应对各种开发场景。
表达式 | 描述 |
---|---|
. |
匹配除换行符以外的任意字符 |
^ |
匹配输入字符串的开始位置 |
$ |
匹配输入字符串的结束位置 |
* |
匹配前一个元素 0 次或多次 |
+ |
匹配前一个元素 1 次或多次 |
? |
匹配前一个元素 0 次或 1 次 |
[] |
匹配括号内的任意字符 |
[^^] |
匹配不在括号内的任意字符 |
{n} |
正好匹配 n 次 |
{n,} |
至少匹配 n 次 |
{n,m} |
匹配 n 到 m 次 |
\d |
匹配一个数字字符,等价于 [0-9] |
\w |
匹配字母或数字或下划线,等价于 [a-zA-Z_0-9] |
\s |
匹配任何空白字符 |
` | ` |
java.util.regex.Pattern
java.util.regex.Matcher
import java.util.regex.*;
public class RegexExample1 {
public static void main(String[] args) {
String pattern = "^[a-zA-Z0-9]{6,12}$";
String input = "abc12345";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
if (m.matches()) {
System.out.println("匹配成功");
} else {
System.out.println("匹配失败");
}
}
}
import java.util.regex.*;
public class RegexExample2 {
public static void main(String[] args) {
String text = "联系邮箱:[email protected], [email protected]";
String regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println("找到邮箱: " + m.group());
}
}
}
import java.util.regex.*;
public class RegexExample3 {
public static void main(String[] args) {
String input = "客户电话:13812345678,备用号码:13987654321";
String regex = "1[3-9]\\d{9}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
String result = m.replaceAll("***保密***");
System.out.println(result);
}
}
public class RegexValidation {
public static void main(String[] args) {
String phone = "13812345678";
String email = "[email protected]";
String idCard = "110101199003077777";
System.out.println(phone.matches("^1[3-9]\\d{9}$")); // true
System.out.println(email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")); // true
System.out.println(idCard.matches("^\\d{17}[\\dXx]$")); // true
}
}
^1[3-9]\\d{9}$
^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$
^\\d{17}[\\dXx]$
^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$
^(\\d{1,3}\\.){3}\\d{1,3}$
^https?://[\\w.-]+(?:\\.[\\w\\.-]+)+[/#?]?.*$