本文仅列举常见正则表达式的实用例子,旨在快速使用,想要了解正则表达式的详细教程、具体含义或进行更深入的学习,建议移步至:正则表达式 – 教程 | 菜鸟教程 (runoob.com)
-
匹配手机号
// 匹配以1开头,第二位是3-9中的一个数字,后面跟着9位数字的手机号
String phoneNumber = "13800138000";
String pattern = "^1[3-9]\\d{9}$";
boolean isMatch = phoneNumber.matches(pattern);
-
匹配QQ号
// 匹配以1-9开头的5-10位数字QQ号
String qq = "1959332124";
String pattern = "^[1-9][0-9]{4,9}$";
boolean isMatch = qq.matches(pattern);
-
匹配身份证号
// 匹配身份证号
String idCard = "11010519491231002X";
String pattern = "^[1-9][0-9]{5}(18|19|([23][0-9]))[0-9]{2}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9Xx]$";
boolean isMatch = idCard.matches(pattern);
-
匹配密码
// 匹配以字母开头,长度在6~18之间,只能包含字母、数字和下划线密码
String password = "Aa1234_";
String pattern = "^[a-zA-Z]\\w{5,17}$";
boolean isMatch = password.matches(pattern);
-
匹配强密码
// 匹配必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间的强密码
String password = "Aa12345@";
String pattern = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$";
boolean isMatch = password.matches(pattern);
-
匹配邮箱地址
// 匹配常见的邮箱地址格式,包括大小写字母、数字、下划线、连字符和点等字符组成的用户名部分,以及@符号和域名部分
String email = "zwz@test.com";
String pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
boolean isMatch = email.matches(pattern);
-
匹配日期格式
// 匹配日期格式'1992-09-03'或'1992.09.03'
String date = "1992-09-03";
String pattern = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
boolean isMatch = date.matches(pattern);
-
匹配IP地址
String ip = "127.0.0.1";
String pattern = "\\d+\\.\\d+\\.\\d+\\.\\d+";
boolean isMatch = ip.matches(pattern);
-
匹配邮政编码
// 匹配6位中国邮政编码
String code = "277000";
String pattern = "[1-9]\\d{5}(?!\\d)";
boolean isMatch = code.matches(pattern);
-
匹配URL
// 匹配常见的URL格式,包括协议部分、域名部分和资源路径部分
String url = "https://www.baidu.com";
String pattern = "^((https?|ftp|file):\\/\\/)?([\\da-z\\.\\-]+)\\.[a-z\\.]{2,6}([\\/\\w\\.\\-]*)*\\/?$";
boolean isMatch = url.matches(pattern);
-
匹配零和非零开头数字
// 匹配零和非零开头的数字
String number = "0";
String pattern = "^(0|[1-9][0-9]*)$";
boolean isMatch = number.matches(pattern);
-
匹配至少n位的数字
// 匹配至少2位的数字
String number = "12";
String pattern = "^\\d{2,}$";
boolean isMatch = number.matches(pattern);
-
匹配m-n位的数字
// 匹配2-4位的数字
String number = "1234";
String pattern = "^\\d{2,4}$";
boolean isMatch = number.matches(pattern);
-
匹配正实数或带两位小数的正实数
// 匹配正实数或带两位小数的正实数 12或12.12
String number = "12.12";
String pattern = "^[0-9]+(.[0-9]{2})?$";
boolean isMatch = number.matches(pattern);
-
匹配由26个英文字母组成的字符串
// 匹配由26个英文字母组成的字符串
String number = "Aa";
String pattern = "^[A-Za-z]+$";
boolean isMatch = number.matches(pattern);
-
匹配由26个大写英文字母组成的字符串
// 匹配由26个大写英文字母组成的字符串
String number = "ABC";
String pattern = "^[A-Z]+$";
boolean isMatch = number.matches(pattern);
-
匹配由26个小写英文字母组成的字符串
// 匹配由26个小写英文字母组成的字符串
String number = "abc";
String pattern = "^[a-z]+$";
boolean isMatch = number.matches(pattern);
-
匹配由数字和26个英文字母组成的字符串
// 匹配由数字和26个英文字母组成的字符串
String number = "Aa1234";
String pattern = "^[A-Za-z0-9]+$";
boolean isMatch = number.matches(pattern);
-
匹配不包含~的字符串
// 匹配不包含~的字符串
String number = "abc";
String pattern = "[^~\\x22]+";
boolean isMatch = number.matches(pattern);
-
匹配HTML标签
// 匹配HTML标签,包括标签名和标签内的属性部分
String label = "<html>";
String pattern = "<([a-z]+)([^>]*)>";
boolean isMatch = label.matches(pattern);
-
从字符串中提取数字
// 从字符串中提取数字
String text = "I have 10 apples and 5 oranges.";
String pattern = "\\d+";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(text);
while (matcher.find()) {
String number = matcher.group();
System.out.println("匹配到的数字: " + number);
}
// 输出:匹配到的数字: 10,匹配到的数字: 5
-
从字符串中提取数据
// 从字符串中提取数据
String csvData = "John,Doe,30\nJane,Smith,25";
String pattern = "(.*),(.*),(\\d+)";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(csvData);
while (matcher.find()) {
String firstName = matcher.group(1);
String lastName = matcher.group(2);
int age = Integer.parseInt(matcher.group(3));
System.out.println("姓名: " + firstName + " " + lastName + ",年龄: " + age);
}
// 输出:姓名: John Doe,年龄: 30,姓名: Jane Smith,年龄: 25
-
替换特定文本
// 替换特定文本
String text = "Hello World!。It's a beautiful world.";
String pattern = "world";
String replacement = "universe";
String modifiedText = text.replaceAll(pattern, replacement);
System.out.println("替换后的文本: " + modifiedText);
// 输出:替换后的文本: Hello Universe!。It's a beautiful universe.
-
替换数字
// 替换数字为指定文本
String text = "Hello, 2023! This is a sample text with some numbers: 12345 and 98765.";
String patternString = "\\d+";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
String modifiedText = matcher.replaceAll("X");
System.out.println("替换后的文本: " + modifiedText);
// 输出:替换后的文本: Hello, X! This is a sample text with some numbers: X and X.
-
从字符串中提取URL链接
// 从字符串中提取URL
String text = "Visit our website at https://www.youkuaiyun.com for more information.";
String pattern = "https?://[\\w\\-.]+(/[\\w/_.]*)?";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(text);
while (matcher.find()) {
System.out.println("URL: " + matcher.group());
}
// 输出:URL: https://www.youkuaiyun.com
-
从日志文件中提取错误日志
// 从日志文件中提取错误日志
String logData = "2023-01-01 12:00:00 ERROR 1001: System failed to start\n" +
"2023-01-01 12:05:00 INFO: System running normally\n" +
"2023-01-01 12:10:00 ERROR 1002: Disk space insufficient";
String pattern = "ERROR \\d+:.*";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(logData);
while (matcher.find()) {
System.out.println("Error log: " + matcher.group());
}
// 输出:Error log: ERROR 1001: System failed to start,Error log: ERROR 1002: Disk space insufficient