package com.iflytek.test.utils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* 数据提取工具类:从一个字符串里提取符合格式类型的数据,如果有多个则返回List,如果没有则返回null
**/
public class DataCollectUtil {
public static List<String> getString(String s, String regex) {
List<String> strs = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while (m.find()) {
strs.add(m.group());
}
return strs;
}
//数值类型字符串:由"0-9"以及".-"构成的数值字符
public static List<String> getSz(String str) {
String regex = "(\\-|\\+)?\\d+(\\.\\d+)?";
return getString(str, regex);
}
//数值百分比类型字符串:由"0-9"以及".-"构成的数值字符,并以"%"结尾
public static List<String> getSzbfb(String str) {
String regex = "(\\-|\\+)?\\d+(\\.\\d+)?%";
return getString(str, regex);
}
//金额类型字符串:由"0-9"以及".-"构成的金额字符
public static List<String> getJe(String str) {
String regex = "(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})";
return getString(str, regex);
}
//金额大写字符串:金额大写中文字符串
public static List<String> getJedx(String str) {
String regex = "[零壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分]+";
return getString(str, regex);
}
//正数字符串:由"0-9"以及"."构成的数值字符
public static List<String> getZs(String str) {
String allRegex = "(\\-){0,1}\\d+(\\.\\d+)?";
List<String> allData = getString(str, allRegex);
String fRegex = "(\\-)\\d+(\\.\\d+)?";
List<String> fData = getString(str, fRegex);
if (!allData.isEmpty()) {
allData.removeAll(fData);
}
return allData;
}
//纯数字字符串:由"0-9"构成的字符串
public static List<String> getCsz(String str) {
String regex = "\\d+";
return getString(str, regex);
}
//纯字符串:不包括空格换行等字符的字符串
public static String getCzf(String str) {
return str.replaceAll(" ", "");
}
//增值税密码区:由"0-9"以及"+-*/<>"等字符构成的字符串
public static List<String> getZzs(String str) {
String regex = "[0-9\\+\\*\\-\\/\\<\\>]+";
return getString(str, regex);
}
public static boolean isDate(String str, String pattern) {
boolean result = true;
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
format.setLenient(false);
format.parse(str);
} catch (Exception e) {
result = false;
}
return result;
}
//日期时间字符串:yyyy-MM-dd HH:mm:ss
public static List<String> getDateTime(String str) {
String regex = "\\d{4}-\\d{1,2}-\\d{1,2}\\s([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
//校验获取的日期时间
List<String> dateTimeList = getString(str, regex);
String pattern = "yyyy-MM-dd HH:mm:ss";
List<String> list;
if (!dateTimeList.isEmpty()) {
list = dateTimeList.stream().filter(s -> DataCollectUtil.isDate(s, pattern)).collect(Collectors.toList());
return list;
} else {
return dateTimeList;
}
}
//日期字符串:yyyy年MM月dd日
public static List<String> getDate(String str) {
String regex = "([1-9]\\d{3}年)(([0]{0,1}[1-9]月)|([1][0-2]月))((0?[1-9]日)|((1|2)[0-9]日)|30日|31日)";
List<String> dateList = getString(str, regex);
//校验日期
String pattern = "yyyy年MM月dd日";
List<String> list;
if (!dateList.isEmpty()) {
list = dateList.stream().filter(s -> DataCollectUtil.isDate(s, pattern)).collect(Collectors.toList());
return list;
} else {
return dateList;
}
}
//时间字符串:HH:mm:ss
public static List<String> getTime(String str) {
String regex = "([0,1]{1,2}[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
return getString(str, regex);
}
//纯英文字符串:由"a-zA-Z"构成的字符串
public static List<String> getCyw(String str) {
String regex = "[A-Za-z]+";
return getString(str, regex);
}
//纯英文大写字符串:由"A-Z"构成的字符串
public static List<String> getCywdx(String str) {
String regex = "[A-Z]+";
return getString(str, regex);
}
//英文数值字符串:由"0-9a-zA-Z"构成的字符串
public static List<String> getYwsz(String str) {
String regex = "[A-Za-z0-9]+";
return getString(str, regex);
}
//车牌号字符串:第一个字符为省简称+5/6位英文数值字符串
public static List<String> getCph(String str) {
String regex = "[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}";
return getString(str, regex);
}
//身份证字符串:符合身份证规则的字符串
public static List<String> getSfz(String str) {
String regex15 = "[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}";
String regex18 = "[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]";
List<String> idCards15 = getString(str, regex15);
List<String> list15 = new ArrayList<>();
if (!idCards15.isEmpty()) {
list15 = idCards15.stream().filter(s -> IdCardUtil.validate15IDCard(s)).collect(Collectors.toList());
}
List<String> idCards18 = getString(str, regex18);
List<String> list18 = new ArrayList<>();
if (!idCards18.isEmpty()) {
list18 = idCards18.stream().filter(s -> IdCardUtil.validate18Idcard(s)).collect(Collectors.toList());
}
List<String> list = new ArrayList<>();
if (!list15.isEmpty()) {
list.addAll(list15);
}
if (!list18.isEmpty()) {
list.addAll(list18);
}
return list;
}
//重量字符串:数值类型字符串 + 单位结尾。单位为'KG'(公斤)、'G'(克)、'T'(吨) |
public static List<String> getZl(String str) {
String regex = "(\\-|\\+)?\\d+(\\.\\d+)?(KG|G|T)";
return getString(str, regex);
}
}
注意:IdCardUtil参考https://blog.csdn.net/zhangleiyes123/article/details/82747228