智能生成和解释正则表达式,支持各种常见模式匹配
将您的文字指令转换为公式,或输入公式获取解释。
通过和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\\.-]+)+[/#?]?.*$