正则表达式(2)

  • String类三个和正则表达式相关的方法

   1.boolean matches

  “abc”.matches(“[a]”) 匹配成功返回true。

 2.String[] split(String 正则的规则)

  “abc”.split(“a”) 使用规则将字符串进行切割。

 3.String replaceAll( 替换字符串)

  “abc0123”.repalceAll(“[d]”,”#”)。

  • 例1 public boolean matches(String regex) 
    校验qq号码. 
    1: 要求必须是5-15位数字; 
    2: 0不能开头。 
1     String qq = "604154942";
2     String regex = "[1-9][0-9]{4,14}";
3     boolean flag2 = qq.matches(regex);
  • 例2 public String[] split(String regex) 
    分割出字符串中的的数字
1     String s = "18-22-40-65";
2     String regex = "-";
3     String[] result = s.split(regex);
  • 例3 public String replaceAll(String regex,String replacement) 
    把文字中的数字替换成*
1     String s = "Hello12345World6789012";
2     String regex = "[0-9]";
3     String result = s.replaceAll(regex, "*");
  •  检验邮箱

  1、 @: 前 数字字母_ 个数不能少于1个

  2、 @: 后 数字字母 个数不能少于1个
  3、 . : 后面 字母

public static void checkMail(){
        String email ="wq341@wssa.com";
        boolean b = email.matches("[a-zA-Z0-9_]+@[0-9a-z]+(\.[a-z]+)+");
        System.out.println(b);
    }
原文地址:https://www.cnblogs.com/Mhang/p/7434825.html