Java语言环境下使用。
电话号码
(\\d{3,4}-)?\\d{6,8}
15位身份证
[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}
18位身份证
[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])
2个字以上汉字
[\\u4e00-\\u9fa5]{2,}
手机号
((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}
车牌号
(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))
日期
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30
测试用的Java代码如下:
public class Test {
public static void main(String[] args) throws ParseException {
String rulePattern = "(\\{[a-zA-Z_]*\\})";
String msg = "时间范围 |{date_range} ";
Pattern p = Pattern.compile(rulePattern);
Matcher matcher = p.matcher(msg);
int matcher_start = 0;
while (matcher.find(matcher_start)){
System.out.println(matcher.group(1));
for (int i = 0; i <= matcher.groupCount(); i ++) {
System.out.println("m.group(" + i + "): " + matcher.group(i));
}
matcher_start = matcher.end();
}
}
}
网友评论