代码如下:
package tk.mybatis.simple.other;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class Test1 {
@Test
public void test1(){
//身份证正则校验
String idNO = "132313190001010000";
System.out.println(checkIdNO(idNO));
//手机号码正则校验
String phone = "13000000000";
System.out.println(checkPhone(phone));
//日期正则校验
String date = "20050228";
System.out.println(checkDate(date));
}
private boolean checkIdNO(String idNO){
String reg = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
Matcher matcher = Pattern.compile(reg).matcher(idNO);
return matcher.matches();
}
private boolean checkPhone(String phone){
String reg = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
Matcher matcher = Pattern.compile(reg).matcher(phone);
return matcher.matches();
}
private boolean checkDate(String date){
String reg = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229)";
Matcher matcher = Pattern.compile(reg).matcher(date);
return matcher.matches();
}
}
控制台输出如下:
true
true
true
判断字符串能否转为小数或整数
package test.demo;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* 测试正则
* @author Administrator
*
*/
public class TestReg {
public static void main(String[] args) {
String content = "-0.20";
System.out.println(checkNumber(content));
new Emp();
List<Emp> list = Arrays.asList(
Emp.builder().name("张三").money("0.15").build(),
Emp.builder().name("李四").money("666").build(),
Emp.builder().name("王五").money("-1.15").build()
);
String re = addNumber(list);
System.out.println(re);
//BigDecimal q1 = new BigDecimal("0");
//BigDecimal q2 = new BigDecimal("0.15");
//System.out.println(q1.add(q2).toString());
}
//stream() − 为集合创建串行流 map 方法用于映射每个元素到对应的结果
//判断某字符串 是否能转为 小数或整数
public static boolean checkNumber(String str) {
if(StringUtils.isBlank(str)) return false;
boolean int_flag = Pattern.compile("^-?[1-9]\\d*$").matcher(str).find();
boolean double_flag = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$").matcher(str).find();
if(double_flag || int_flag) return true;
return false;
}
//BigDecimal 求和
public static String addNumber(List<Emp> list) {
BigDecimal total = list.stream().map(x -> new BigDecimal(x.getMoney())).reduce(BigDecimal.ZERO, BigDecimal::add);
return total.toString();
}
public void checkNumber666(String str){
if(null == str || "" == str){
System.out.println("字符串为空");
}
boolean int_flag = Pattern.compile("^-?[1-9]\\d*$").matcher(str).find();
boolean double_flag = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$").matcher(str).find();
if(int_flag){
System.out.println("字符串类型为int类型");
} else if(double_flag){
System.out.println("字符串类型为double类型");
} else {
System.out.println("字符串类型为其他类型");
}
}
}
控制台输出如下
true
665.00