一.正则表达式
1.正则表达式的校验
1)典型顺序
Pattern p = Pattern.compile("a*b") ;
Matcher m = p.matcher("aaaaaaaab") ;
boolean flag = m.matches() ;
通过Pattern模式类中compile()要获取模式对象,通过里面的matcher()方法获取匹配器对象Matcher,再去使用
2)简化版
String regex = "a*b" ;
String s = "aaaab" ;
boolean flag = s.matches(regex) ;
2.正则校验手机号
String phone = sc.nextLine();
boolean flag = phone.matches(regex) ;
3.正则校验邮箱号
String email = sc.nextLine() ;
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+" ;
boolean flag = email.matches(regex) ;
4.替换正则表达式的子字符串
String s = "hello1234567world6891234java" ;
String regex = "[0-9]+";
String regex = "\\d+" ;
String ss = "*" ;
String result = s.replaceAll(regex, ss) ;
5. 根据给定正则表达式的匹配切割此字符串
String s = "aa,bb,cc" ;
String[] strArray = s.split(",") ;
for(int x = 0 ; x < strArray.length ; x++){
System.out.println(strArray[x]);}
6.例题