93java正则表达式(String类对正则的支持

69 阅读3分钟

【第09个代码模型】正则表达式(String类对正则的支持)

 

  • String类对正则的支持等示例

从JDK1.4开始String类发生了一些改变,用户可以直接通过String类来进行正则的调用,

在String类里有如下几个直接支持正则开发的方法:

No.方法名称类型描述 
01Public boolean matches(String regex)普通       进行字符串验证,匹配某个正则 
02Public String replaceAll(String regex,String replacement)  普通根据正则的描述替换全部 
03Public String replaceFirst(String regex,String replacement)普通根据正则替换首个 
04Public String[] split(String regex)普通根据正则拆分 
05Public String[] split(String regex,int limit)普通根据正则拆分 

 

范例:实现字符串替换

public static void main(String[] args) throws Exception{

   String str = “….”;

   String regex = “[^a-zA-Z]”;//正则

   System.out.println(str.replaceAll(regex,””));

}

}

 

范例:字符串拆分


public static void main(String[] args) throws Exception{

   String str = “….”;

   String regex = “\\d+”;//正则[0-9]

   String result[] = str.split(regex);

   for (int x= 0; x<result.length;x++){

        System.out.println(result[x]);

}

}

}

 

范例:验证某个字符串是否是数字(整数或小数),如果是则将其变为Double型

 

public static void main(String[] args) throws Exception{

   String str = “11010.8989;

   String regex = “\\d+(\\.\\d+)?”;//正则[0-9]

   if (str.matches(regex)){ //匹配成功

   double data = Double.parseDouble(str);

   System.out.println(data);

}

}

}

 

范例:要求验证一个字符串是否是日期或者是日期时间,如果是则将其变成Date类型

public static void main(String[] args) throws Exception{

      String str = “2018-10-02  10:11:11”;

      String regex = “\\d{4}-\\d{2}-\\d{2}”;

      if (str.matches(regex)){ //匹配成功

        System.out,println(new SimpleDateFormat(“yyyy-MM-dd”).parse(str));

} else if(str.matches(“\\d{4}-\\d{2}-\\d{2}-\\d{2}-\\d{2}-\\d{2}”)){

        System.out.println(new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”). parse(str));

}

}

}

 

此时这种需要转换的前提下,对于正则的格式判断最好分开编写

 

范例:现在要求判断给定的电话号码是否正确

. 电话格式一:

51283346[\d{7,8}];

. 电话格式二:

01051283346[(\d{3,4})?\d{7,8}];

. 电话格式三:

(010)-51283346[((\(\d{3,4}\)-)|(\d{3,4}))?\{7,8}]。

 

public static void main(String[] args)throws Exception{

     String str = “51283346;

     String regex = “\\d{7,8}”;

     System.out.println(str.matches(regex));

}

}

 

public static void main(String[] args)throws Exception{

     String str = “01051283346;

     String regex = “[(\\d{3,4})](https://hanlin.aliyun-inc.com/(/d%7b3,4%7d))?\\d{7,8}”;

     System.out.println(str.matches(regex));

}

}

 

public static void main(String[] args)throws Exception{

     String str = “(010)-51283346;

     String regex = “((\\(\\d{3,4}\\)-)|(\\d{3,4}))?\\{7,8}”;

     System.out.println(str.matches(regex));

}

}

 

范例:验证给定的字符串是否是email地址

. 简单验证:邮件地址由字母、数字、_所组成;


public static void main(String[] args)throws Exception{

     String str = “a@a.a”;

     String regex = “\\w+@\\w+\\.\\w+”;

     System.out.println(str.matches(regex));

}

}

    

  .email的用户名必须由字母开头,而且组成可以是字母、数字、_、-、.所组成,而且用户名的长度必须在6-15位之间,域名的后缀必须是.com、.net、.cn、.org、.gov、.com.cn.、net.cn。

 

结构关系图:

 

 

public static void main(String[] args)throws Exception{

     String str = “amldn88-1.2@mldnjava.cn”;

     String regex = “[a-zA-Z][a-zA-Z\\._-0-9]{5,14}@[a-zA-Z\\._-0-9]+\\.(com|net|org|com\\.cn)”;

     System.out.println(str.matches(regex));

}

}