Home > Java > javaTutorial > body text

Summary of regular verification frequently used in Java development

无忌哥哥
Release: 2018-07-23 10:32:59
Original
1558 people have browsed it

This article mainly summarizes the commonly used regular verification in Java

1. Mobile phone number verification

    public static boolean isMobile(String str) {
        Pattern p;
        Matcher m;        boolean b;
        p = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();        
        return b;
    }
Copy after login

2. Determine whether the obj object attributes are empty. If they are all empty, If it is not empty, it will return false

    public static boolean checkFieldValueNull(Object obj){
        Class<?> clazz = obj.getClass();        
        for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
            Field[] fields=clazz.getDeclaredFields();            
                for(Field field:fields){                
                    try {
                    field.setAccessible(true);                    
                    if("serialVersionUID".equals(field.getName())){                        
                        continue;
                    }                    
                    if (field.get(obj) != null) {//判断字段是否为空,并且对象属性中的基本都会转为对象类型来判断
                        return false;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }        
        return true;
    }
Copy after login

3. Determine whether the URL contains http://. If not, it will automatically add

url = url.substr(0,7).toLowerCase() == "http://" ? url : "http://" + url;
Copy after login
to the URL.

The above is the detailed content of Summary of regular verification frequently used in Java development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template