A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© chengaq0 中级黑马   /  2015-12-16 23:18  /  957 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

正则表达式
    是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。
    A.字符类
        [abc] a、b 或 c(简单类)
        [^abc] 任何字符,除了 a、b 或 c(否定)
        [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
        [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
        [a-z&&[def]] d、e 或 f(交集)
        [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
        [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
  字符类例题:
ession constructs, and what they match" border="0">ession constructs, and what they match" border="0">B.预定义字符类
        .任何字符(与行结束符可能匹配也可能不匹配)
        \d数字:[0-9]
        \D非数字: [^0-9]
        \s空白字符:[ \t\n\x0B\f\r]
        \S非空白字符:[^\s]
        \w单词字符:[a-zA-Z_0-9]
        \W非单词字符:[^\w]



预定义类例题:
ession constructs, and what they match" border="0">C.Greedy 数量词
        X?X,一次或一次也没有
        X*X,零次或多次
        X+X,一次或多次
        X{n}X,恰好 n
        X{n,}X,至少 n
        X{n,m}X,至少 n 次,但是不超过 m

/**
*    Greedy 数量词
*    X? X,一次或一次也没有
*    X* X,零次或多次
*    X+ X,一次或多次
*    X{n} X,恰好 n 次
*    X{n,} X,至少 n 次
*    X{n,m} X,至少 n 次,但是不超过 m 次
*/
public class Demo04_Regex2 {
public static void main(String[] args) {
        //X? X,一次或一次也没有 ,没有指的是前面没有字符
        String regex1 = "\\d?"; // \\d?等价于[0-9]? [0-9]出现过一次或一次也没有
        System.out.println("1".matches(regex1)); //true 出现一次
        System.out.println("a".matches(regex1)); //false 不在\d范围内
        System.out.println("".matches(regex1)); //true 一次也没有
        System.out.println("1a".matches(regex1)); //false 两个字符超出范围
        System.out.println("-----------------------------------");
        //X* X,零次或多次
        String regex2 = "\\d*"; //[0-9]出现0次或多次
        System.out.println("10".matches(regex2)); //true 出现2次
        System.out.println("112abc".matches(regex2)); //false 出现了范围之外的值
        System.out.println("-----------------------------------");
        //X+ X,一次或多次
        String regex3 = "\\w+"; //[a-zA-Z_0-9]+
        System.out.println("abcd0_5AZ".matches(regex3)); //true
        System.out.println("ab%+".matches(regex3)); //false
        System.out.println("-----------------------------------");
        //X{n} X,恰好 n 次
        String regex4 = "\\w{5}"; //等价于[a-zA-Z_0-9]{5}
        System.out.println("abcd1".matches(regex4)); //true
        System.out.println("abc%+".matches(regex4)); //false
        System.out.println("-----------------------------------");
        //X{n,} X,至少 n 次
        String regex5 = "[a-zA-Z]{5,}";
        System.out.println("abcdfewaf".matches(regex5)); //true
        System.out.println("abdfe%+".matches(regex5)); //false
        System.out.println("-----------------------------------");
        //X{n,m} X,至少 n 次,但是不超过 m 次
        String regex6 = "[0-9]{5,13}";
        System.out.println("12321".matches(regex6));
        System.out.println("1213111121112".matches(regex6));
        }
}
正则表达式分割字符串例题:

正则表达式替换字符例题:



2 个回复

倒序浏览
学习下,正则表达式,基本上都是现用,现查
回复 使用道具 举报
你们等级以及技术分是怎么快速获得的呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马