加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

达内课程-正则表达式

发布时间:2020-12-14 06:43:40 所属栏目:百科 来源:网络整理
导读:Regex正则表达式 正确的字符串格式规则 一般用来判断用户的输入格式 正则表达式 匹配的字符串 k k abc abc [abc] […]表示字符集,是其中一个即可:a/b/c [abc][123] a1/a2/a3/b1/b2/b3/c1/c2/c3 [a-z] a/b/c/d…z [a-zA-Z0-9] a/A/z/Z/0/9.._ [^a-zA-Z] ^表

Regex正则表达式

正确的字符串格式规则
一般用来判断用户的输入格式

正则表达式 匹配的字符串
k k
abc abc
[abc] […]表示字符集,是其中一个即可:a/b/c
[abc][123] a1/a2/a3/b1/b2/b3/c1/c2/c3
[a-z] a/b/c/d…z
[a-zA-Z0-9] a/A/z/Z/0/9.._
[^a-zA-Z] ^表示排除一个范围:排除英文字母
[u4e00-u9fa5] 中文范围
d 数字[0-9]
D 排除数字[^0-9]
w 单词字符[a-zA-Z_0-9]
W 排除单词字符[^a-zA-Z_0-9]
s 空白字符:回车/换行/制表符/……
S 排除空白字符
. 任意字符
[abc]? ?表示0个或1个
[abc]?[123] 1/2/3/a1/a2…
[abc]* *表示0到多个
[abc]*[123] 1/2/3/a1/aaabccba1/…
[abc]+ +表示1到多个
[abc]+[123] a1/ab2/abcbcac1/…
[abc]{3} {}大括号表示固定数量:aaa/bca/cbc/…
[abc]{3,5} 3-5个:acc/abca/abcab/…
[abc]{3,} 3到多个:abc/abcccccaaaa/…
| 或,匹配左右其中一个表达式即可

相关方法

matches(正则表达式)

判断当前字符串,是否匹配正则表达式

栗子

public class Test {
    public static void main(String[] args) {  
        System.out.println("输入身份证号:");
        String s = new Scanner(System.in).nextLine();

        /* * 123456789012345 * 123456789012345678 * 12345678901234567x * 12345678901234567X * * d{15}|d{17}[dxX] * 需要对“”进行转义 * -> * \-> */
        String regex = "d{15}|d{17}[dxX]";

        if(s.matches(regex)){
            System.out.println("格式正确");
        }else{
            System.out.println("格式错误");
        }
    }  
}

栗子

public class Test {
    public static void main(String[] args) {  
        System.out.println("输入固定电话:");
        String s = new Scanner(System.in).nextLine();

        /* * 123456 * 1234567 * 12345678 * (010)12345678 * (0102)12345678 * 010-123456 * 0102-1234567 * * (d{3,4}-|(d{3,4}))?d{6,8} */
        String regex = "(d{3,8}";

        if(s.matches(regex)){
            System.out.println("格式正确");
        }else{
            System.out.println("格式错误");
        }
    }  
}

replaceAll(正则表达式,子串)

将找到的匹配子串,替换为新的子串

public class Test {
    public static void main(String[] args) {  
        System.out.println("言论自由:");
        String s = new Scanner(System.in).nextLine();

        String regex = "草泥马|尼玛|尼妹|傻逼";

        s = s.replaceAll(regex,"***");
        System.out.println(s);
    }  
}

运行结果

言论自由:
广电太尼玛傻逼了
广电太**

split(正则表达式)

用匹配的子串,拆分字符串

public class Test {
    public static void main(String[] args) {  
        System.out.println("输入关键词列表,用逗号、分号、空格分隔");
        String s = new Scanner(System.in).nextLine();

        String regex = "[,; ]+";

        String[] a = s.split(regex);
        for(int i=0;i<s.length();i++){
            System.out.println(a[i]);
        }
    }  
}

java.util.regex.Pattern 和java.util.regex.Matcher

Pattern 封装正则表达式
Matcher 封装正则表达式,和要匹配的字符串

创建实例

Pattern p = Pattern.compile(正则表达式);
Matcher m = p.matcher(要匹配的字符串);

Matcher方法

find()
向后查找下一段匹配的子串
返回boolean值表示是否找到

find(int from)
从指定位置向后查找

group()
提取刚刚找到的子串

start()
end()
刚刚找到的子串的起始位置和结束位置

栗子
匹配字符串中的3到多个数字

public class Test {
    public static void main(String[] args) {  
        System.out.println("输入:");
        String s = new Scanner(System.in).nextLine();

        //3到多个连续数字
        String regex = "d{3,}";

        Matcher m = Pattern.compile(regex)
                    .matcher(s);

        //一直向后查找,直到false
        while(m.find()){
            String s2 = m.group();
            int start = m.start();
            int end = m.end();
            System.out.println(start+"-"+end+":"+s2);
        }
    }  
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读