1. Java method to determine whether a string is an integer or a decimal:
matches() method: used to detect whether a string matches a given regular expression.
EX: str.matches("regular expression")
regular expression:
//判断是否是整数或4位内的小数
[+-]?[0-9]+(\\.[0-9]{
1,4})?
//判断是否是整数或不限制小数位的小数
[+-]?[0-9]+(\\.[0-9]+)?
2. Mobile phone number legality verification
Verification of mainland mobile phone number:
package com.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* 手机号校验工具类
*
* @author Duncino
* @date 2021/12/23
*/
public class PhoneCheckUtils {
/**
* 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
*/
public static boolean isPhoneLegal(String phone) throws PatternSyntaxException {
String regex = "^((13[0-9])|(14[05679])|(15([0-3,5-9]))|(16[2567])|(17[01235678])|(18[0-9]|19[135689]))\\d{8}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
return isMatch;
}
}
3. Email regular expression
Email verification rules:
- There must be content before @ and it can only be letters (uppercase and lowercase), numbers, underscores (_), minus signs (-), and dots (.)
- There must be content between @ and the last dot (.) and it can only be letters (upper and lower case), numbers, dots (.), and minus signs (-), and the two dots cannot be next to each other.
- There must be content after the last dot (.) and the content can only be letters (upper and lower case), numbers, and the length is greater than or equal to 2 bytes and less than or equal to 6 bytes.
Regular expression for email verification:
^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{
2,6}$