认识正则表达式
jdk1.4以后正则表达式已被jdk支持,并且由java.util.regex开发包提供,String类也进行了修改。
可以通过百度搜索邮箱等正则表达式验证。
优点:方便进行验证处理,以及复杂字符串修改处理。
public class RegExTest {
public static void main(String[] args) {
String regex="[abc]";
String str="a";
System.out.println(str.matches(regex));
}
}
常用正则标记
java.util.regex.Pattern类中定义了所有正则标记。
- 单个字符匹配:
字符 | 表示含义 |
---|---|
任意字符 | 确定的字符 |
\\ | 匹配“\” |
\n | 换行 |
\t | 制表符 |
[abc] | 字母abc中任意一个字符 |
[^abc] | 除abc的任意字符 |
[a-d] | a到d之间的任意字符,闭合区间 |
[a-dA-D] | a到d 的任意一个字符,不区分大小写 |
public class RegExTest {
public static void main(String[] args) {
String regex="[0-9]";
String str="8";
System.out.println(str.matches(regex));
}
}
- 简化字符串
字符 | 表示含义 |
---|---|
. | 任意一个字符 |
\\d | 任意一位数字 |
\\D | [^0-9] |
\\s | 任意空字符(制表符,回车,空格) |
\\S | 任意非空字符 |
\\w | 匹配字母,数字,下划线。等价于“[a-zA-Z_0-9]” |
\\W | 等价于“[^a-zA-Z_0-9]” |
- 边界匹配:(java中用不到)
字符 | 表示含义 |
---|---|
^ | 匹配边界开始 |
$ | 匹配边界结束 |
- 数量表示:默认情况下只有添加上了数量单位才可以匹配多个字符
字符 | 表示含义 |
---|---|
表达式? | 该正则可出现零次或一次 |
表达式* | 该正则可以出现0次,1次或多次 |
表达式+ | 可以出现1次或多次 |
表达式{n} | 表达式正好n次 |
表达式{n,} | 表达式n次以上 |
表达式{n,m} | 表达式出现n到m次 |
- 逻辑表达式:可以连接多个正则
字符 | 表示含义 |
---|---|
表达式x表达式y | x表达式之后紧跟y表达式 |
表达式x|表达式y | 满足一个表达式即可 |
(表达式) | 为表达式设置一个整体描述,可为整体描述设置数量单位 |
String类对正则表达式的支持
- 将指定字符串进行正则判断:public boolean matches(String regex)
- 替换全部:public String replaceAll(String regex,String replacement)
- 替换首个:public String replaceFirst(String regex,String replacement)
- 正则拆分:public String[] split(String regex)
- 拆分成指定的个数:public String[] split(String regex,int limit)
范例:实现字符串的替换
public class RegExTest {
public static void main(String[] args) {
String regex="[^a-zA-Z_0-9]+";
String str="045-h_*^)**O%DVBncouehtw5t5+519t/-4g+q4y+5y5+879+9o1p'+8`41";
System.out.println(str.replaceAll(regex,""));
}
}
范例:实现字符串的拆分
public static void main(String[] args) {
//String regex="\\d";//"[^a-zA-Z]+";
String regex="\\d+";
String str="a161513b34165c3879844d186183541e987641f698461g879";
String arr[]=str.split(regex);
for(int x=0;x<arr.length;x++) {
System.out.print(arr[x]+"-");
}
}
“\d”是一个一个拆分的,“\d+”是批处理的
范例:判断一个数据是否为小数,如果是小数则将变成double型
public class RegExTest {
public static void main(String[] args) {
//String regex="\\d";//"[^a-zA-Z]+";
String regex="\\d+(\\.\\d*)?";//允许100->100.0
String str="123456.789";
System.out.print(str.matches(regex));
}
}
范例:判断是否为日期类型
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class RegExTest {
public static void main(String[] args) throws ParseException {
String regex="\\d{4}-\\d{2}-\\d{2}";
String str="2015-54-22";
if(str.matches(regex)) {
System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));
}
}
}
但是只能进行格式处理,不能判断内容处理
范例:判断电话号码是否正确
6515063:\\d{7,8}
03506515063:(\\d{3,4})?\\d{7,8}
(0350)-6515063:((\\d{3,4})|\\((\\d{3,4})\\))?\\d{7,8}
范例:验证email格式
email的用户名:字母,数字,_
email的域名:字母,数字,_,-
域名后缀:.cn,.com,.net,.com.cn,.gov;
"[a-zA-Z0-9]\\w+@\\w+\\.(cn|com|com.cn|gov)"
java.util.regex包支持
Pattern类(正则表达式编译类),Matcher(匹配)。
Pattern类
构造方法私有化,提供了正则表达式的编译处理:public static Pattern compile(String regex);
字符串的拆分操作:public String[] split(CharSequence input);
import java.util.regex.Pattern;
public class RegExTest {
public static void main(String[] args){
String regex="[^\\w]+";
String str="sd382452823+5*-3g5g+as5f-q43568o5580*95--0*8]'5l";
Pattern pat=Pattern.compile(regex);//编译正则表达式
String result[]=pat.split(str);
for(int x=0;x<result.length;x++) {
System.out.println(result[x]);
}
}
}
Matcher类
实现了正则匹配的处理类,这个类的对象实例化依靠Pattern类
- Pattern类提供的方法:
public Matcher matcher(CharSequence input);
当获取了类对象后,就可以利用该方法进行操作: - 正则匹配:
public boolean matches();
- 字符串的替换:
public String replaceAll(String replacement);
字符串匹配:
import java.util.regex.*;
public class RegExTest {
public static void main(String[] args){
String regex="[\\w]+";
String str="sd3824528233g5gas5fq43568o5580l";
Pattern pat=Pattern.compile(regex);//编译正则表达式
Matcher mat=pat.matcher(str);
System.out.println(mat.matches());
}
}
如果以拆分,替换,匹配三种操作为例,用不到java.util.regex包
Matcher类中提供了一种分组的功能,String中没有。
范例:取出字符串中的某些数据:
import java.util.regex.*;
public class RegExTest {
public static void main(String[] args){
String regex="#\\{\\w+\\}";
//取出#{}中的全部内容
String str="INSERT INTO dept(deptno,dname,loc) VALUES (#{deptno},#{dname},#{loc})";
Pattern pat=Pattern.compile(regex);
Matcher mat=pat.matcher(str);
while(mat.find()) {
System.out.println(mat.group().replaceAll("[#\\{\\}]", ""));
}
}
}
日后进行实体层框架处理时,需要用到。
java.util.regex包用在复杂的正则操作中,String类用于处理简单的正则操作。而正则验证是最主要的功能。