标签:
正则表达式可以对字符串进行查找、提取、分割、替换等操作。String类下提供了如下几个特殊的方法:
1、boolean matches(String regex):判断该字符串是否匹配指定的正则表达式
2、String replaceAll(String regex, String replacement):将该字符串所有匹配regex的字串替换成replacement
3、String replaceFirst(String regex, String replacement):将该字符串中第一个匹配regex的字串替换成replacement
4、String[] split(String regex):以regex作为分隔符,把该字符串分割成多个字串
Java还提供了Pattern和Matcher两个类专门用于提供正则表达式支持:java.util.regex包下
正则表达式是一个用于匹配字符串的模板
//将一个字符串编译成Pattern对象
//将标准通过Pattern.compile()方法编译到一个对象 Pattern p = Pattern.compile("a*b"); //使用Pattern对象创建Matcher对象
//利用Pattern创建对应的Matcher对象,执行匹配所涉及的状态保留在Matcher对象中,多个Matcher对象可以共享一个Pattern对象 Matcher m = p.matcher("aaaaab"); boolean b = m.matches();
上面程序的Pattern对象可以多次重复使用。如果某个正则表达式仅需一次使用,则可以直接使用Pattern类的静态matches()方法,此方法自动把指定字符串编译成匿名Pattern对象,并执行匹配
boolean b = Pattern.matches("a*b", "aaaaab");
Pattern是不可变类,可供多个并发线程安全使用。
Matcher类提供了几个常用方法:
1、find():返回目标字符串是否包含与Pattern匹配的子串
2、group():返回上一次与Pattern匹配的子串
3、start():返回上一次与Pattern匹配的子串在目标字符串开始的位置
4、end():返回上一次与Pattern匹配的子串在目标字符串的结束位置加1
5、lookAt():返回目标字符串前面部分与Pattern是否匹配
6、matches():返回整个目标字符串与Pattern是否匹配
7、rest():将现有的Matcher对象应用于一个新的字符序列
注:matches与find同时使用会影响,所有要用rest()方法
import java.util.regex.*; public class MatcherTest{ public static void main(String[] args){ String s = "我的联系方式:13536635431"; Pattern p = Pattern.compile("((13\\d)|(15\\d))\\d{8}"); Matcher m = p.matcher(s); //matches方法不能和group方法同时使用,不过利用reset()方法可以让它们同时使用 //System.out.println(m.matches()); //判断是否包含与标准匹配的字串 while(m.find()){ //打印匹配的字串 System.out.println(m.group()); } } }
为了让matches和fina同时使用,可改为
Matcher n = m.reset();
System.out.println(n.matches());
import java.util.regex.*; public class MatcherTest{ public static void main(String[] args){ String s = "我的联系方式:13536635431,他的电话13453634234"; Pattern p = Pattern.compile("((13\\d)|(15\\d))\\d{8}"); Matcher m = p.matcher(s); //将所有符合正则表达式的子串全部输出 while(m.find()){ System.out.println(m.group()); } } }
find()方法还可以传入一个int类型的参数,带int参数的find()方法将从int索引往下搜索,
start()和end()方法主要用于确定子串在目标字符串中的位置
import java.util.regex.*; public class MatcherTest{ public static void main(String[] args){ String str = "Java is very hard"; System.out.println("目标字符串是:" + str); Matcher m = Pattern.compile("\\w+").matcher(str); while(m.find()){ System.out.println(m.group() + "子串的起始位置: "+ m.start() + "子串的结束位置: " + m.end() ); } } }
matches()和lookingAt()方法有点相似,matches()方法要求整个字符串与标准完全匹配,lookingAt()方法只要字符串以Pattern开头就会返回true。rest()方法可将现有的Matcher对象应用于新的字符串序列。
import java.util.regex.*; public class MatcherTest{ public static void main(String[] args){ //待匹配数组 String[] mail = {"dasfga@qq.com", "14123423@163.com", "7678fg@123.com"}; //编译标准 Pattern p = Pattern.compile("\\w{3,20}@\\w+\\.(com|org|cn|net|gov)"); //定义Matcher对象,赋初值为空值 Matcher m = null; //迭代数组 for(String s : mail){ //当Matcher对象为空时,就创建一个Matcher对象 if(m == null){ m = p.matcher(s); } else{ //如果已经创建了Matcher实例,就调用rest()方法将该Matcher应用于新的字符序列 m.reset(s); } String result = s + (m.matches() ? "是" : "不是") + "一个有效的邮箱"; System.out.println(result); } } }
匹配部分改为下面这样也可以
Pattern p = Pattern.compile("\\w{3,20}@\\w+\\.(com|org|cn|net|gov)"); for(String s : mail){ Matcher m = p.matcher(s); String result = s + (m.matches() ? "是" : "不是") + "一个有效的邮箱"; System.out.println(result); }
String类也提供matches方法
"14123423@163.com".matches("\\w{3,20}@\\w+\\.(com|org|cn|net|gov)");
Matcher类提供了replaceAll()把字符串所有与正则表达式匹配的字串替换掉,Matcher实例名.replaceAll("替换字符");把所有与标准匹配的替换掉。
还有String类提供的几个方法,也可以在正则表达式中对匹配进行操作。
标签:
原文地址:http://www.cnblogs.com/changzuidaerguai/p/5668087.html