Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
怎么在Android开发中实现一个正则匹配功能 - 源码之家

怎么在Android开发中实现一个正则匹配功能

这篇文章给大家介绍怎么在Android开发中实现一个正则匹配功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

在Android开发中,可能也会遇到一下输入框的合法性验证,这时候最常用的就应该是正则表达式去做一些匹配了,下面就常用的正则匹配做一下介绍

1. 手机号码的验证

根据实际开发于2009年9月7日最新统计: 中国电信发布中国3G号码段:中国联通185,186;中国移动188,187;中国电信189,180共6个号段。

移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188

联通:130、131、132、152、155、156、185、186

电信:133、153、180、189、(1349卫通)

匹配代码(目前,号码段可能添加了一下,大家根据实际情况写正则表达式)

public class ClassPathResource {
 private static final Logger logger = Logger.getLogger(ClassPathResource.class);
  public static boolean isMobileNO(String mobiles){
    Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
    Matcher m = p.matcher(mobiles);
    logger.info(m.matches()+"---");
    return m.matches();
  }

2.邮件的验证

public static boolean isEmail(String email){
String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
  Pattern p = Pattern.compile(str);
  Matcher m = p.matcher(email);
  logger.info(m.matches()+"---");
  return m.matches();

3.IP地址的验证

Pattern pattern = Pattern.compile("");
Matcher matcher = pattern.matcher("127.400.600.2"); //以验证127.400.600.2为例
System.out.println(matcher.matches());

关于怎么在Android开发中实现一个正则匹配功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。