Android开发正则表达式
时间: 2023-11-03 10:02:56 浏览: 36
正则表达式在 Android 开发中非常常见,可以用来进行字符串匹配、替换、验证等操作。以下是一些常见的正则表达式:
1. 匹配手机号码:
```
String regex = "^1[3-9]\\d{9}$";
boolean isMatch = Pattern.matches(regex, phoneNumber);
```
2. 匹配邮箱地址:
```
String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
boolean isMatch = Pattern.matches(regex, email);
```
3. 匹配身份证号码:
```
String regex = "^\\d{17}[\\d|x|X]$";
boolean isMatch = Pattern.matches(regex, idCardNumber);
```
4. 匹配 URL 地址:
```
String regex = "^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
+ "(([0-9]{1,3}\\.){3}[0-9]{1,3}"
+ "|"
+ "([0-9a-z_!~*'()-]+\\.)*"
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\."
+ "[a-z]{2,6})"
+ "(:[0-9]{1,4})?"
+ "((/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
boolean isMatch = Pattern.matches(regex, url);
```
5. 匹配 IP 地址:
```
String regex = "^((\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}(\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])$";
boolean isMatch = Pattern.matches(regex, ipAddress);
```
以上是一些常见的正则表达式,在实际开发中可以根据需求进行修改和扩展。