[Java] 正则表达式
正则表达式
- 字符串可以保存任意的数据信息,并向所有的数据类型转换
- 正则最早是Linux下发展起来的技术,理论基础源自离散数学
- JDK 1.4 修改了String类定义,可通过字符串类进行正则的处理
- 传统的做法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Regex_Demo { 2 public static void main(String[] args) throws Exception{ 3 String str = "123abc"; 4 System.out.println(isNumber(str)); 5 } 6 7 public static boolean isNumber(String temp) { 8 if(temp == null || "".equals(temp)) { 9 return false; 10 } 11 char data[] = temp.toCharArray(); 12 for(int x = 0 ; x < data.length; x++) { 13 if(data[x]>'9' || data[x] < '0') { 14 return false; 15 } 16 }return true; 17 } 18 }
- 正则表达式做法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Regex_Demo { 2 public static void main(String[] args) throws Exception{ 3 String str = "123abc"; 4 System.out.println(str.matches("\\d+")); 5 } 6 }
正则标记
- 单字符匹配
- x:表示任意的一位字符
- \\:匹配任意的一位“\”(Java中转义字符)
- \t:匹配制表符(Java中转义字符)
- \n:匹配换行(Java中转义字符)
- 字符范围(未加入任何量词描述,只表示1位)
- [abc]:匹配字母“a”、“b”、“c”中任意一位
- [^abc]:求反,匹配字母不是“a”、“b”、“c”中任意一位
- [a-zA-Z]:匹配所有字母(大小写)
- [0-9]:匹配所有数字
- 简化表达式(未加入任何量词描述,只表示1位)
- .:表示任意的字符
- \d:匹配任意的一位数字,等价于[0-9]
- \D:匹配任意一位非数字,等价于^[0-9]
- \s:空格
- \S:匹配任意的非空格,等价于[^\s]
- \w:匹配字母(大小写)、数字、下划线,等价于[a-zA-Z_0-9]
- \W:匹配非字母(大小写)、数字、下划线
- 边界匹配
- ^:匹配开始位置
- $:匹配结束位置
- 数量表达式
- 正则表达式?:表示该正则表达式出现0次或者1次
- 正则表达式*:表示该正则表达式出现0次、1次或者多次
- 正则表达式+:表示该正则表达式出现1次或者多次
- 正则表达式{n}:表示正则表达式正好出现n次
- 正则表达式{n,}:表示正则表达式出现n次以上
- 正则表达式{n,m}:表示正则表达式出现n~m次
- 逻辑运算(连接多个正则表达式)
- 正则表达式A正则表达式B:在A之后紧跟B正则
- 正则表达式A|正则表达式B:正则A或正则B有一组满足
- (正则):将多个正则表达式作为一组出现,可为这组单独设置出现的次数
String类对正则的支持
- public boolean matches(String regex):正则验证,判断是否符合给定的正则表达式结构
- public String replaceAll(String regex, String replacement):替换全部匹配字符
- public String replaceFirst(Stirng regex, String replacement):替换首个匹配的字符
- public String[] split(String regex):按指定的正则标记将字符串全部拆分
- public String[] split(String regex, init limit):按指定的正则标记将字符串部分拆分
验证一个字符串是否是数字,如果是则将其变为double型
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Regex_Demo { 2 public static void main(String[] args) throws Exception{ 3 String str = "10.10"; 4 String regex = "\\d+(\\.\\d+)?"; 5 if(str.matches(regex)) { 6 System.out.println(Double.parseDouble(str)); 7 } 8 } 9 }
判断给定的字符串是否是一个IP地址
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Regex_Demo { 2 public static void main(String[] args) throws Exception{ 3 String str = "192.168.1.1"; 4 String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; 5 System.out.println(str.matches(regex)); 6 } 7 }
判断是否是日期格式,否则转换为Date型数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Regex_Demo { 2 public static void main(String[] args) throws Exception{ 3 String str = "2020-03-09"; 4 String regex = "\\d{4}-\\d{2}-\\d{2}"; 5 if(str.matches(regex)) { 6 Date date = new SimpleDateFormat("yyyy-MM-dd").parse(str); 7 System.out.println(date); 8 } 9 } 10 }
判断电话号码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Regex_Demo { 2 public static void main(String[] args) throws Exception{ 3 String str = "(010)-58343546"; 4 String regex = "((\\d{3,4}-)|(\\(\\d{3,4}\\)-))?\\d{7,8}"; 5 System.out.println(str.matches(regex)); 6 } 7 }
判断e-mail
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Regex_Demo { 2 public static void main(String[] args) throws Exception{ 3 String str = "mldn.100_lixinghua@yootk.com"; 4 String regex = "[a-zA-Z][a-zA-Z0-9_\\.]{0,28}[a-zA-Z0-9]" 5 +"@\\w+\\.(com|net|cn|com\\.cn|net\\.cn|org|gov|edu)"; 6 System.out.println(str.matches(regex)); 7 } 8 }
java.util.regex包支持
- JDK 1.4后提供正则开发包:java.util.regex,包中提供两个核心的类:Matcher(匹配)、Pattern(表达式处理)
- Pattern
- public static Pattern compile ( String regex )
- public String[] split ( CharSequence input )
- public String[] split ( CharSequence input, int limit )
- public Matcher matcher ( CharSequence input )
- Pattern类没有构造方法,想取得Pattern类对象,必须利用compile()方法进行正则表达式的编译操作
- 所有实现了CharSequence的子类均可使用,CharBuffer、Segment、String、StringBuffer、StringBuilder
- Matcher
- public boolean matches()
- public String replaceAll ( String replacement )
- public String replaceFirst ( String replacement )
- 利用Pattern类的matcher()方法取得Matcher类对象,然后利用matches()方法判断字符串是否符合正则规范
- String无法完成一些复杂的正则操作,只能依赖Pattern 和 Matcher