十:正则表达式

          引言:通过之前一些列的分析可以发现,String是一个非常万能的类型,因为String不仅仅可以支持有各种字符串的处理,也支持有向各个数据类型的转换功能,所以通常以String类型来接收用户的输入。而在向其他数据类型进行转换的时候,为了保证转换的正确性,往往需要对其进行一些复杂的验证处理,而String类中提供的方法并不能很友好的进行验证,所以就有了正则的出现。

 

1、认识正则表达式

          范例:传统方法判断字符串是不是数字

代码结果

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str = "123";
        if (isNumber(str)) {
            int num = Integer.parseInt(str);
            System.out.println(num * 2);
        }
    }

    // 定义一个判断方法
    public static boolean isNumber(String str) {
        char data[] = str.toCharArray();
        for (int x = 0; x < data.length; x++) {
            // 如果里面的数字不在字符串的0~9的值之间,则返回false,判断字符不仅是数字
            if (data[x] > '9' || data[x] < '0') {

            }
        }
        return true;
    }

}
 

246

          从上面的程序中分析,可以得知一个简单的验证,需要写如上那么多的逻辑程序代码,那么在后期的开发之中,会有更多的验证,代码相比只会更多,所以,为了验证的方便,引入了正则验证。

          范例:正则表达式判断字符串是不是数字

 代码结果
纯数字验证

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str = "123";
        // 使用正则表达式:“\\d+”表示纯数字
        if (str.matches("\\d+")) {
            int num = Integer.parseInt(str);
            System.out.println(num * 2);
        }
        else{
            System.out.println("我不是数字");
        }
    }

}
 

246
非纯数字验证

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str = "12x";
        // 使用正则表达式:“\\d+”表示纯数字
        if (str.matches("\\d+")) {
            int num = Integer.parseInt(str);
            System.out.println(num * 2);
        }
        else{
            System.out.println("我不是数字");
        }
    }

}
 

我不是数字
 

          从上面程序可以看出,使用了正则表达式,代码量明显比传统判断少了很多。

          注意,在JDK1.4版本之前,使用正则需要引入相关的*.jar文件,而在JDK1.4版本之后,正则是被默认支持的,则不需要引入*.jar包,并且提供了专门的java.util.regex开发包,且对于String类也进行了一些修改,使其可以有方法直接支持正则处理。

          而使用正则最大的特点在于方便进行验证处理,以及方便进行复杂字符串的修改。

 

2、常用正则标记

          若要熟练的使用正则操作,须得掌握其常有的正则标记。

          在java.util.regex这个包里提供了一个Pattern程序类,定义了所有支持的正则定义,部分如下,详情可见regex包中的Pattern:

                    1)字符匹配——字符表示的是单个字符,不是字符串

Characters含义代码结果描述
x单个字符

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str = "a";
        // 正则表达式定义为“a”,表示只有在匹配到单个字符“a”的时候才会为true
        String regex = "a";
        System.out.println(str.matches(regex));
    }

}
 

true正则表达式定义为“a”,表示只有在匹配到单个字符“a”的时候才会为true
\\\  表示单个“\”
\n换行  匹配换行
\t制表符  匹配制表符

                    2)字符集匹配——从里面任选一个字符进行匹配

字符集匹配含义代码结果描述
[abc]表示可能是字母中a、b、c中的任意一个

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str = "a";
        // 正则表达式
        String regex = "[abc]";
        System.out.println(str.matches(regex));
    }

}
 

true
 
只要定义的字符在"[]"中存在,那么返回结果一定是ture。
[^abc]表示可能不是字母中a、b、c中的任意一个

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str = "d";
        // 正则表达式
        String regex = "[^abc]";
        System.out.println(str.matches(regex));
    }

}
 

true只要定义的字符不在"[]"中存在,那么返回结果一定是ture。
[a-zA-Z]表示由一个任意字母所组成,不区分大小写

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str = "1";
        // 正则表达式
        String regex = "[a-zA-Z]";
        System.out.println(str.matches(regex));
    }

}
 

false
 
表示除此范围外的都返回false
[0-9]表示由一位数字组成

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str = "1";
        // 正则表达式
        String regex = "[0-9]";
        System.out.println(str.matches(regex));
    }

}
 

true
 
如果不是单个数字,则会返回false

                    3)简化字符集

简化字符集含义代码结果描述
.表示任意的一个字符

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "1";
        String str2 = "a";
        String str3 = "#";

        // 正则表达式
        String regex = ".";
        System.out.println(str1.matches(regex));
        System.out.println(str2.matches(regex));
        System.out.println(str3.matches(regex));
    }

}
 

true
true
true
 
单个数字、字母、符号等都表示一个字符
\d表示数字

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "1";
        String str2 = "a";

        // 正则表达式
        String regex = "\\d";
        System.out.println(str1.matches(regex));
        System.out.println(str2.matches(regex));
    }

}
 

true
false
 
等价于[0-9]范围定义
\D表示不是数字

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "1";
        String str2 = "a";

        // 正则表达式
        String regex = "\\D";
        System.out.println(str1.matches(regex));
        System.out.println(str2.matches(regex));
    }

}
 

false
true
 
等价于[^0-9]
\s匹配任意的一位空格

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "a ";
        String str2 = "a\t";
        String str3 = "a";

        // 正则表达式
        String regex = "\\D\\s";
        System.out.println(str1.matches(regex));
        System.out.println(str2.matches(regex));
        System.out.println(str3.matches(regex));
    }

}
 

true
true
false
 
可能是空格、换行、制表符
\S匹配任意的非空格数据

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "a ";
        String str2 = "a\t";
        String str3 = "a";

        // 正则表达式
        String regex = "\\S";
        System.out.println(str1.matches(regex));
        System.out.println(str2.matches(regex));
        System.out.println(str3.matches(regex));
    }

}
 

false
false
true
 
不包含空格、换行、制表符
\w匹配字母、数字、下划线  等价于:[a-zA-Z_0-9]
\W匹配非字母、数字、下划线  [^a-zA-Z_0-9]

                    4)边界匹配

边界匹配含义
^匹配边界开始
$匹配边界结束

                    5)数量表示,默认情况下只有添加上了数量单位才可以匹配多位字符:

正则数量含义代码结果
表达式?表示该正则可以出现1次或0次

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "$$a$";
        String str2 = "";
        String str3 = "abxc";

        // 正则表达式——判断字符出现一次或0次
        String regex1 = "\\w?";
        // 正则表达式——判断字符出现一次或0次或多次
        String regex2 = "\\w*";
        // 正则表达式——判断字符出一次或多次
        String regex3 = "\\w+";
        System.out.println(str1.matches(regex1));
        System.out.println(str2.matches(regex2));
        System.out.println(str3.matches(regex3));
    }

}
 

false
true
true
 
表达式*表示该正则可以出现0次、1次或多次
表达式+表示该正则可以出现1次或多次
表达式{n}表示表达式的长度正好为n次

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "abcd";
        String str2 = "ab";
        String str3 = "abcde";

        // 正则表达式——判断字符出现一次或0次
        String regex1 = "\\w{4}";
        // 正则表达式——判断字符出现一次或0次或多次
        String regex2 = "\\w{4,}";
        // 正则表达式——判断字符出一次或多次
        String regex3 = "\\w{3,5}";
        System.out.println(str1.matches(regex1));
        System.out.println(str2.matches(regex2));
        System.out.println(str3.matches(regex3));
    }

}
 

true
false
true
 
表达式{n,}表示表达式的长度为n次以上
表达式{n,m}表示表达式的长度在n~m次

                    6)逻辑表达式:可以连接多个正则

逻辑表达式含义代码结果描述
表达式X表达式Y表示X表达式之后紧跟着Y表达式

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要判断的单个数据为“a”
        String str1 = "ab";

        // 正则表达式
        String regex1 = "ab";

        System.out.println(str1.matches(regex1));
    }

}
 

true
 
表示字母a后面跟着字母b
表达式X|表达式Y有一个表达式满足即可   
(表达式)为表达式设置一个整体描述,可以为整体描述设置数量单位

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "ababab";
        String str2 = "abab";

        // 正则表达式——判断“ab”是否出现了3次
        String regex1 = "(ab){3}";

        System.out.println(str1.matches(regex1));
        System.out.println(str2.matches(regex1));
    }

}
 

true
false
 
判断”ab“是否出现了三次

 

3、String类对正则的支持

          在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)拆分成指定的个数

          1)范例实现字符串的替换——删除掉非字母数字的内容

代码结果

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "a123444hn##$*&sjgAHF&^^$#$VV";

        String regex1 = "[^a-zA-Z0-9]+";

        System.out.println(str1.replaceAll(regex1, ""));
    }

}
 

a123444hnsjgAHFVV
 

          2)范例:实现字符串的拆分——按数字拆分,提取字母

 代码结果区别
不加量词

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "ahgsb111khgsn6676sjhgsb8897sghshd54r";

        String regex1 = "\\d";
        String result[] = str1.split(regex1);
        for (int x = 0; x < result.length; x++) {
            System.out.print(result[x] + "、");
        }
    }

}
 

ahgsb、、、khgsn、、、、sjhgsb、、、、sghshd、、r、不加两次是一个一个进行比较,加了两次是一起进行比较
加量词

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "ahgsb111khgsn6676sjhgsb8897sghshd54r";

        String regex1 = "\\d+";
        String result[] = str1.split(regex1);
        for (int x = 0; x < result.length; x++) {
            System.out.print(result[x] + "、");
        }
    }

}
 

ahgsb、khgsn、sjhgsb、sghshd、r、
 

          2)范例:判断一个数据类型是否为小数,如果是小数则将其变为double类型

代码结果

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "100.32";
        String str2 = "100";

        // 正则表达式
        String regex1 = "\\d+(\\.\\d+)?";
        System.out.println(str1.matches(regex1));
        System.out.println(str2.matches(regex1));
    }

}
 

true
true

          3)范例:判断一个字符串是否由日期组成,如果由日期组成,则转为Date类型

代码结果

package cn.demos;

 

import java.text.SimpleDateFormat;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "1998-10-10";

        // 正则表达式
        String regex1 = "\\d{4}-\\d{2}-\\d{2}";
        // 如果符合条件,则转为日期型
        if (str1.matches(regex1)) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str1));
        }

       
    }

}
 

Sat Oct 10 00:00:00 CST 1998
 

          注意:正则只能校验格式是否符合规定,并不能判断里面的数值是否符合规定。即:”2020-23-32‘这类格式也是正确的。

          4)范例:判断给定的电话号码是否正确

                    如下格式:

                                        1234567、0101234567、(010)-1234567

代码结果

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "1234567";
        String str2 = "0101234567";
        String str3 = "(010)-1234567";

        // 正则表达式
        String regex1 = "((\\d{3,4})|(\\(\\d{3,4}\\)-))?\\d{7,8}";
        // 如果符合条件,则转为日期型

        System.out.println(str1.matches(regex1));
        System.out.println(str2.matches(regex1));
        System.out.println(str3.matches(regex1));
    }

}
 

true
true
true
 

          5)范例:验证email格式

                    a.email的用户名可以由字母、数字、_组成(不应该使用下划线开头);

                    b.email的域名可以由字母、数字、_、-所组成

                    c.email的后缀必须是:.cn、.com、.net、.com.cn、.gov;

代码结果

package cn.demos;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "csdnemail@csdn.cn";
        String str2 = "csdnemail@csdn.abc";

        // 正则表达式
        String regex1 = "[a-zA-Z0-9]\\w+@\\w+\\.(cn|com|com.cn|net|gov)";
        // 如果符合条件,则转为日期型

        System.out.println(str1.matches(regex1));
        System.out.println(str2.matches(regex1));
    }

}
 

true
false
 

 

 

4、java.util.regex包支持

          在大部分情况下我们可以利用String类实现正则的操作,但是也有一些情况下需要使用java.util.regex开发包中的正则处理类。在这个包中,总共定义了两个类:Pattern(正则表达式编译类)、Matcher(正则匹配类)。

          1)Parrern类中包含如下方法:

方法定义代码结果
正则表达式的编译
public static Pattern compile​(String regex)

package cn.demos;

import java.util.regex.Pattern;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "jahdg()akhgaad@ajdahs$dgf";

        // 正则表达式
        String regex1 = "[^a-zA-Z]+";

        Pattern pat = Pattern.compile(regex1);
        String result[] = pat.split(str1);

        for (int x = 0; x < result.length; x++) {
            System.out.println(result[x]);
        }

        // System.out.println(str1.matches(regex1));

    }

}
 

jahdg
akhgaad
ajdahs
dgf
 
字符串的拆分操作
public String[] split​(CharSequence input)

          2)Matcher类,实现了正则匹配的处理类,这个类的对象实例化依靠Pattern类完成:

                    注意:在Pattern类中提供了 :public Matcher matcher​(CharSequence input)方法。

          所以,当获取了Matcher类的对象之后就可以利用该类中的方法进行如下操作:

方法定义代码结果
正则匹配
public boolean matches​()

package cn.demos;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        String str1 = "101";

        // 正则表达式
        String regex1 = "\\d+";

        // 正则编译
        Pattern pat = Pattern.compile(regex1);
        Matcher mat = pat.matcher(str1);

        System.out.println(mat.matches());

    }

}

true
 
字符串替换
public String replaceAll​(String replacement)

 

 

          通过上面的程序分析,我们可以得知使用String类就可以进行拆分、替换、匹配等这三种操作,可以不用使用这两个类。但其实这两个类除了提供上面三种操作外,还具备有一种分组功能是String类不具备的。
         

          范例:正则分组

代码结果

package cn.demos;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // 要求取出“#{内容}”标记的内容
        String str = "INSERT INTO dept(deptno,dname,loc) VALUES (#{deptno},#{dname},#{loc})";

        // 正则表达式
        String regex = "#\\{\\w+\\}";

        // 正则编译
        Pattern pat = Pattern.compile(regex);
        Matcher mat = pat.matcher(str);

        // 是否有匹配成功的
        while (mat.find()) {
            System.out.println(mat.group(0).replaceAll("#|\\{|\\}", ""));
        }
    }

}
 

deptno
dname
loc
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值