正则表达式在线测试——菜鸟工具:https://c.runoob.com/front-end/854/
1. 密码校验
^.*(?=.{
8,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[~!@#$%^&*()_+|<>,.?/:;'\[\]{}" ]).*$
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main(String args[]) {
String password= "12345678!Qass";
String pattern = "^.*(?=.{8,})(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\" ]).*$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(password);
System.out.println(m.matches());
}
}
2. 电话号码校验(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号)
((\d{
11})|^((\d{
7,8})|(\d{
4}|\d{
3})-(\d{
7,8})|(\d{
4}|\d{
3})-(\d{
7,8})-(\d{
4}|\d{
3}|\d{
2}|\d{
1})|(\d{
7,8})-(\d{
4}|\d{
3}|\d{
2}|\d{
1}))$)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main(String args[]) {
String str = "17755555555";
String pattern = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d)|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d))$)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.matches());
}
}
3. 座机电话号码校验(0530-2536987,021-87888822)
\d{
3}-\d{
8}|\d{
4}-\d{
7}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main(String args[]) {
String str = "0530-2536987";
String pattern = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.matches());
}
}
4. 邮箱校验
^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main(String args[]) {
String str = "[email protected]";
String pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.matches());
}
}
5. 更多正则表达式参考网站
-
- 菜鸟工具: https://c.runoob.com/front-end/854/
-
- 菜鸟教程:https://www.runoob.com/regexp/regexp-example.html
-
- GitHub仓库: https://github.com/Janson404/learn-regex-zh