Java在处理一些复杂的字符串操作时,往往不是通过String中函数实现的,而是通过Java中正则表达式实现的。
下面通过一个具体的例子简单的介绍一下Java中正则表达式具体的用法。
package myapi.regex.demo;
public interface RegularInterface {
public boolean checkDigit(String input);
public boolean checkMail(String input);
public boolean checkPhone(String input);
public boolean checkIDCard(String input);
public boolean checkDate(String input);
public boolean checkDateTime(String input);
}
package myapi.regex.demo;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import myapi.time.demo.JavaTimeApi;
public class MyRegexApi implements RegularInterface {
public MyRegexApi() {
}
private static Map<String,String>map=new HashMap<String,String>();
static{
map.put("11","北京");map.put("12","天津");map.put("13","河北");map.put("14","山西");map.put("15","内蒙古");
map.put("21","辽宁");map.put("22","吉林");map.put("23","黑龙江");map.put("31","上海");map.put("32","江苏");
map.put("33","浙江");map.put("34","安徽");map.put("35","福建");map.put("36","江西");map.put("37","山东");
map.put("41","河南");map.put("42","湖北");map.put("43","湖南");map.put("44","广东");map.put("45","广西");
map.put("46","海南");map.put("50","重庆");map.put("51","四川");map.put("52","贵州");map.put("53","云南");
map.put("54","西藏");map.put("61","陕西");map.put("62","甘肃");map.put("63","青海");map.put("64","宁夏");
map.put("65","新疆");map.put("71","台湾");map.put("81","香港");map.put("82","澳门");map.put("91","国外");
}
public static void main(String[] args) {
MyRegexApi m = new MyRegexApi();
System.out.println(m.checkIDCard("340822198007265519"));
}
/**
* @author qiang
* @param input
* 输入条件
* @return boolean 判断输入条件是否是数字,是返回true,不是返回false
*/
@Override
public boolean checkDigit(String input) {
Pattern p = Pattern.compile("^[0-9]{1,20}$");
Matcher m = p.matcher(input);
return m.matches();
}
@Override
public boolean checkMail(String input) {
Pattern p = Pattern
.compile("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
Matcher m = p.matcher(input);
return m.matches();
}
@Override
public boolean checkPhone(String input) {
Pattern p = Pattern.compile("^1[3|4|5|7|8]\\d{9}$");
Matcher m = p.matcher(input);
return m.matches();
}
@Override
public boolean checkIDCard(String input) {
boolean flags[]={checkIDCard_specification(input),checkIDCard_Province(input),
checkIDCard_checkBirthday(input),checkIDCard_checkParity(input)};
for(int i=0;i<flags.length;i++){
if(flags[i]==false){
return false;
}
}
return true;
}
public boolean checkIDCard_specification(String input){
Pattern p = Pattern.compile("(^\\d{15}$)|(^\\d{17}(\\d|X)$)");
Matcher m = p.matcher(input);
return m.matches();
}
public boolean checkIDCard_Province(String input){
if(map.get(input.substring(0,2))!=null && map.containsKey(input.substring(0,2))){
return true;
}else{
return false;
}
}
public boolean checkIDCard_checkBirthday(String input){
Pattern p=null;
Matcher m=null;
if(input.length()==15){
p = Pattern.compile("^(\\d{6})(\\d{2})(\\d{2})(\\d{2})(\\d{3})$");
m = p.matcher(input);
if(m.matches()){
int year=Integer.valueOf("19"+input.substring(6, 8));
int month=Integer.valueOf(input.substring(8, 10));
int day=Integer.valueOf(input.substring(10, 12));
return checkIDCard_Birthday(year,month,day);
}else{
return false;
}
}else if(input.length()==18){
p = Pattern.compile("^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$");
m = p.matcher(input);
if(m.matches()){
int year=Integer.valueOf(input.substring(6, 10));
int month=Integer.valueOf(input.substring(10, 12));
int day=Integer.valueOf(input.substring(12, 14));
return checkIDCard_Birthday(year,month,day);
}else{
return false;
}
}else{
return false;
}
}
public boolean checkIDCard_Birthday(int year,int month,int day){
Calendar cal = Calendar.getInstance();
int lastDay=JavaTimeApi.getLastDayOfMonth(year, month);
int currentYear=cal.get(Calendar.YEAR);
if((currentYear-year<3) || (currentYear-year>101)){
return false;
}else if(month<1 || month>12){
return false;
}else if(day<1 || day>lastDay){
return false;
}
return true;
}
public boolean checkIDCard_checkParity(String input){
if(input.length()==15){
input=checkIDCard_changeNumber(input);
}
int arrInt[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char arrCh[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int cartTemp=0,valnum=0;
for(int i = 0; i < 17; i ++){
cartTemp+=Integer.valueOf(input.charAt(i)+"")*arrInt[i];
}
valnum=arrCh[cartTemp%11];
if(valnum==input.charAt(17)){
return true;
}else{
return false;
}
}
public String checkIDCard_changeNumber(String input){
int arrInt[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char arrCh[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
String obj=input.substring(0, 6)+"19"+input.substring(6, input.length());
int cartTemp=0;
for(int i = 0; i < 17; i ++){
cartTemp+=Integer.valueOf(input.charAt(i))*arrInt[i];
}
obj+=arrCh[cartTemp%11];
return obj;
}
@Override
public boolean checkDate(String input) {
Pattern p = Pattern
.compile("((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(10|12|0?[13578])([-\\/\\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(11|0?[469])([-\\/\\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(0?2)([-\\/\\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([3579][26]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$))");
Matcher m = p.matcher(input);
return m.matches();
}
@Override
public boolean checkDateTime(String input) {
if(input.split(" ").length!=2){
return false;
}
String datas=input.split(" ")[0];
String times=input.split(" ")[1];
Pattern p_datas = Pattern.compile("((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(10|12|0?[13578])([-\\/\\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(11|0?[469])([-\\/\\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(0?2)([-\\/\\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([3579][26]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$))");
Matcher m_datas = p_datas.matcher(datas);
Pattern p_times = Pattern.compile("^(([01]?[0-9])|(2[0-3])):[0-5]?[0-9]:[0-5]?[0-9]$");
Matcher m_times = p_times.matcher(times);
if(m_datas.matches() && m_times.matches()){
return true;
}else{
return false;
}
}
}
上面的例子详细的介绍了,在Java中如何通过使用正则表达式进行电话号码,身份证,日期,数字,邮箱,URL等的验证。