java正则化表达式例子
时间: 2023-07-12 07:59:55 浏览: 42
以下是一些常见的 Java 正则表达式示例:
1. 匹配邮箱地址
```
String regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
String email = "example123@gmail.com";
boolean isMatch = email.matches(regex);
```
2. 匹配 URL
```
String regex = "^(http|https)://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}(/[a-zA-Z0-9-._?,'+&%$#=~]*)?$";
String url = "http://www.example.com/page.html?id=123";
boolean isMatch = url.matches(regex);
```
3. 匹配手机号码
```
String regex = "^1[3-9]\\d{9}$";
String phone = "13812345678";
boolean isMatch = phone.matches(regex);
```
4. 匹配身份证号码
```
String regex = "^\\d{17}[\\d|x|X]$";
String idCard = "33038219900101001X";
boolean isMatch = idCard.matches(regex);
```
5. 匹配日期格式(yyyy-MM-dd)
```
String regex = "^\\d{4}-\\d{2}-\\d{2}$";
String date = "2021-08-01";
boolean isMatch = date.matches(regex);
```