在Eclipce中的使用
public static void main(String[] args) {
// 要验证的字符串
String str = "22bb23";
// 正则表达式规则
String regEx = "\\d+";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
// 查找字符串中是否有匹配正则表达式的字符/字符串,并打印出来
if(matcher.find()){
System.out.println(matcher.group());
}
}
常用的正则表达式
规则 |
正则表达式语法 |
一个或多个汉字 |
+$ |
邮政编码 |
\d{5}$ |
QQ号码 |
\d{4,10}$ |
邮箱 |
{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}.){1,3}[a-zA-z-]{1,}$ |
用户名(字母开头 + 数字/字母/下划线) |
[A-Za-z1-9_-]+$ |
手机号码 |
^1[3 |
URL |
^((http |
18位身份证号 |
^(\d{6})(18 |
参考1
参考2