目录
- Java正则表达式——group方法
- 例子Demo1
- 例子Demo2
- Java正则表达式校验实例
- 1 通过正则表达式制作短信模板
- 2 正则表达式校验身份证
- 总结
Java正则表达式——group方法
捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。
例如,正则表达式 (dog) 创建了单一分组,组里包含"d","o",和"g"。
捕获组是通过从左至右计算其开括号来编号。例如,在表达式((A)(B(C))),有四个这样的组:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
可以通过调用 matcher 对象的 groupCount 方法来查看表达式有多少个分组。groupCount 方法返回一个 int 值,表示matcher对象当前有多个捕获组。
在Java正则表达式的相关类Matcher中,有如下几个方法:
- int groupCount() | |
- String group(int group) | |
- int start(int group) | |
- int end(int group) | |
- String group(String name) | |
- int start(String name) | |
- int end(String name) |
例子Demo1
String text = "John writes about this, and John Doe writes about that," | |
+ " and John Wayne writes about everything."; | |
String patternString = "(John) (.+?) "; | |
Pattern pattern = Pattern.compile(patternString); | |
Matcher matcher = pattern.matcher(text); | |
matcher.find();//匹配字符串,匹配到的字符串可以在任何位置 | |
int start = matcher.start();//返回当前匹配到的字符串在原目标字符串中的位置 | |
int end = matcher.end();//返回当前匹配的字符串的最后一个字符在原目标字符串中的索引位置 | |
System.out.println("found group: group() is '" + matcher.group(0)); | |
System.out.println("found group: group() is '" + matcher.group(1) + "',group(2) is '" + matcher.group(2)+"'"); | |
patternString= "(?:John)"; | |
Pattern pattern = Pattern.compile(patternString); | |
Matcher matcher = pattern.matcher(text); | |
System.out.println("groupCount is -->" + matcher.groupCount()); | |
while (matcher.find()) { | |
System.out.println("found: " + matcher.group()); | |
} |
运行结果:
found group: group(0) is ‘John writes
found group: group(1) is ‘John’,group(2) is ‘writes’
groupCount is –>0
Exception in thread “main” java.lang.IndexOutOfBoundsException: No group 1
从输出结果可以看出,当正则表达式包含多个group时,也就是含有多个’(pattern)’格式的子表达式时,它的分组索引(group number)是从1开始的,而group(0)代表了整个匹配的字符串.
总结:
(1)正则表达式中以'()'标记的子表达式所匹配的内容就是一个分组(group),分组索引是从1开始的,0代表正则表达式匹配的整个字符串,group(i)代表第i组匹配的内容。
(2)groupCount() 函数返回当前正则表达式中分组的个数。
(3)类似于(?:pattern)格式的子表达式不能算是一个分组。
例子Demo2
String text = "John writes about this, and John Doe writes about that," | |
+ " and John Wayne writes about everything."; | |
String patternString = "(John) (.+?) "; | |
Pattern pattern = Pattern.compile(patternString); | |
Matcher matcher = pattern.matcher(text); | |
matcher.find();//匹配字符串,匹配到的字符串可以在任何位置 | |
int start = matcher.start();//返回当前匹配到的字符串在原目标字符串中的位置 | |
System.out.println(start);// | |
int end = matcher.end();//返回当前匹配的字符串的最后一个字符在原目标字符串中的索引位置 | |
System.out.println(end);// | |
start = matcher.start();//第一个分组匹配的内容,也就是John开始的索引位置,0 | |
System.out.println(start);// | |
start = matcher.start();//第一个分组匹配的内容,也就是writes开始的索引位置,5 | |
System.out.println(start);// | |
end = matcher.end();//第一个分组匹配的内容,也就是John结束的索引位置,4 | |
System.out.println(end);// | |
end = matcher.end();//第二个分组匹配的内容,也就是writes开始的索引位置,12 | |
System.out.println(end);// | |
start = matcher.start();//Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 3 |
总结:
(1)当索引大于正则表达式中实际存在的索引数量,也就是groupCount()返回值时会抛出异常。
(2)int start(int group) 返回当前分组匹配到的字符串在原目标字符串中的位置。
(3)返回当前分组匹配的字符串的最后一个字符在原目标字符串中的索引位置。
Java正则表达式校验实例
1 通过正则表达式制作短信模板
1.1 java 替换 ${xxx} 的内容
private static String parse(String content,Map<String,String> kvs){ | |
Matcher m = p.matcher(content); | |
StringBuffer sr = new StringBuffer(); | |
while(m.find()){ | |
String group = m.group(); | |
m.appendReplacement(sr, kvs.get(group)); | |
} | |
m.appendTail(sr); | |
return sr.toString(); | |
} | |
public static void main(String[] args) { | |
Map<String,String> m=new HashMap<>(); | |
m.put("${a}","han"); | |
m.put("${b}","zhong"); | |
System.out.println( parse("例如有这样一个${a}字符串字符串:用户'${a}'的名称${b}", m)); | |
} |
1.2 java正则表达式appendReplacement和appendTail方法
appendReplacement是java中替换相应字符串的一个方法
appendReplacement(StringBuffer sb,String replacement)
将当前匹配子串替换为指定字符串,并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个 StringBuffer 对象里
appendTail(StringBuffer sb)
将最后一次匹配工作后剩余的字符串添加到一个 StringBuffer 对象里
如果没有理解的话,那就来一个简单的demo吧
public class TheReplacements { | |
public static void main(String[] args) throws Exception { | |
// 生成 Pattern 对象并且编译一个简单的正则表达式"cat" | |
Pattern p = Pattern.compile("cat"); | |
// 用 Pattern 类的 matcher() 方法生成一个 Matcher 对象 | |
Matcher m = p.matcher("fatcatfatcatfat"); | |
StringBuffer sb = new StringBuffer(); | |
while(m.find()){ | |
//此时sb为fatdogfatdog,cat被替换为dog,并且将最后匹配到之前的子串都添加到sb对象中 | |
m.appendReplacement(sb,"dog"); | |
} | |
//此时sb为fatdogfatdogfat,将最后匹配到后面的子串添加到sb对象中 | |
m.appendTail(sb); | |
//输出内容为fatdogfatdogfat | |
System.out.println("sb:"+sb); | |
} | |
} |
1.3 正则表达式matcher.group()用法
package cn.oldlu.regexp.singlecharacter; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class GroupIndexAndStartEndIndexTest { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
String str = "Hello,World! in Java."; | |
Pattern pattern = Pattern.compile("W(or)(ld!)"); | |
Matcher matcher = pattern.matcher(str); | |
while(matcher.find()){ | |
System.out.println("Group:"+matcher.group(0));//得到第0组——整个匹配 | |
System.out.println("Group:"+matcher.group(1));//得到第一组匹配——与(or)匹配的 | |
System.out.println("Group:"+matcher.group(2));//得到第二组匹配——与(ld!)匹配的,组也就是子表达式 | |
System.out.println("Start:"+matcher.start(0)+" End 0:"+matcher.end(0));//总匹配的索引 | |
System.out.println("Start:"+matcher.start(1)+" End 1:"+matcher.end(1));//第一组匹配的索引 | |
System.out.println("Start:"+matcher.start(2)+" End 2:"+matcher.end(2));//第二组匹配的索引 | |
System.out.println(str.substring(matcher.start(),matcher.end(1)));//从总匹配开始索引到第1组匹配的结束索引之间子串——Wor | |
} | |
} | |
} |
运行结果:
Group 0:World!
Group 1:or
Group 2:ld!
Start 0:6 End 0:12
Start 1:7 End 1:9
Start 2:9 End 2:12
Wor
2 正则表达式校验身份证
身份证号码验证
1、号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码
2、地址码(前六位数)表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行
3、出生日期码(第七位至十四位)表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符
4、顺序码(第十五位至十七位)表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号, 顺序码的奇数分配给男性,偶数分配给女性
5、校验码(第十八位数)
(1)十七位数字本体码加权求和公式 S = Sum(iDCardNo * wf), i = 0, … , 16 ,先对前17位数字的权求和 iDCardNo:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 wf: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2)计算模 Y = mod(S, 11) (3)通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2
import org.junit.Test; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
import java.util.Hashtable; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class JunitIDCardTest { | |
public void test(){ | |
System.out.println(IdentityCardVerification("")); | |
} | |
/** | |
*身份证验证 | |
* @param idStr | |
* @return | |
*/ | |
public static String IdentityCardVerification(String idStr){ | |
String[] wf = { "", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" }; | |
String[] checkCode = { "", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" }; | |
String iDCardNo = ""; | |
try { | |
//判断号码的长度位或18位 | |
if (idStr.length() != && idStr.length() != 18) { | |
return "身份证号码长度应该为位或18位"; | |
} | |
if (idStr.length() ==) { | |
iDCardNo = idStr.substring(, 17); | |
} else if (idStr.length() ==) { | |
iDCardNo = idStr.substring(, 6) + "19" + idStr.substring(6, 15); | |
} | |
if (isStrNum(iDCardNo) == false) { | |
return "身份证位号码都应为数字;18位号码除最后一位外,都应为数字"; | |
} | |
//判断出生年月 | |
String strYear = iDCardNo.substring(, 10);// 年份 | |
String strMonth = iDCardNo.substring(, 12);// 月份 | |
String strDay = iDCardNo.substring(, 14);// 月份 | |
if (isStrDate(strYear + "-" + strMonth + "-" + strDay) == false) { | |
return "身份证生日无效"; | |
} | |
GregorianCalendar gc = new GregorianCalendar(); | |
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); | |
if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > || (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) { | |
return "身份证生日不在有效范围"; | |
} | |
if (Integer.parseInt(strMonth) > || Integer.parseInt(strMonth) == 0) { | |
return "身份证月份无效"; | |
} | |
if (Integer.parseInt(strDay) > || Integer.parseInt(strDay) == 0) { | |
return "身份证日期无效"; | |
} | |
//判断地区码 | |
Hashtable h = GetAreaCode(); | |
if (h.get(iDCardNo.substring(, 2)) == null) { | |
return "身份证地区编码错误"; | |
} | |
//判断最后一位 | |
int theLastOne =; | |
for (int i =; i < 17; i++) { | |
theLastOne = theLastOne + Integer.parseInt(String.valueOf(iDCardNo.charAt(i))) * Integer.parseInt(checkCode[i]); | |
} | |
int modValue = theLastOne %; | |
String strVerifyCode = wf[modValue]; | |
iDCardNo = iDCardNo + strVerifyCode; | |
if (idStr.length() == &&!iDCardNo.equals(idStr)) { | |
return "身份证无效,不是合法的身份证号码"; | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
/** | |
* 地区代码 | |
* @return Hashtable | |
*/ | |
private static Hashtable GetAreaCode() { | |
Hashtable hashtable = new Hashtable(); | |
hashtable.put("", "北京"); | |
hashtable.put("", "天津"); | |
hashtable.put("", "河北"); | |
hashtable.put("", "山西"); | |
hashtable.put("", "内蒙古"); | |
hashtable.put("", "辽宁"); | |
hashtable.put("", "吉林"); | |
hashtable.put("", "黑龙江"); | |
hashtable.put("", "上海"); | |
hashtable.put("", "江苏"); | |
hashtable.put("", "浙江"); | |
hashtable.put("", "安徽"); | |
hashtable.put("", "福建"); | |
hashtable.put("", "江西"); | |
hashtable.put("", "山东"); | |
hashtable.put("", "河南"); | |
hashtable.put("", "湖北"); | |
hashtable.put("", "湖南"); | |
hashtable.put("", "广东"); | |
hashtable.put("", "广西"); | |
hashtable.put("", "海南"); | |
hashtable.put("", "重庆"); | |
hashtable.put("", "四川"); | |
hashtable.put("", "贵州"); | |
hashtable.put("", "云南"); | |
hashtable.put("", "西藏"); | |
hashtable.put("", "陕西"); | |
hashtable.put("", "甘肃"); | |
hashtable.put("", "青海"); | |
hashtable.put("", "宁夏"); | |
hashtable.put("", "新疆"); | |
hashtable.put("", "台湾"); | |
hashtable.put("", "香港"); | |
hashtable.put("", "澳门"); | |
hashtable.put("", "国外"); | |
return hashtable; | |
} | |
/** | |
* 判断字符串是否为数字 | |
* @param str | |
* @return | |
*/ | |
private static boolean isStrNum(String str) { | |
Pattern pattern = Pattern.compile("[-9]*"); | |
Matcher isNum = pattern.matcher(str); | |
if (isNum.matches()) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* 判断字符串是否为日期格式 | |
* @param strDate | |
* @return | |
*/ | |
public static boolean isStrDate(String strDate) { | |
Pattern pattern = Pattern.compile("^((\\d{}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$"); | |
Matcher m = pattern.matcher(strDate); | |
if (m.matches()) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |