正则表达式

预定义字符类

.任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]

        System.out.println("a".matches("."));
        System.out.println("1".matches("\\d"));
        System.out.println("%".matches("\\D"));
        System.out.println("\r".matches("\\s"));
        System.out.println("^".matches("\\S"));
        System.out.println("a".matches("\\w"));

Greedy 数量词

X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好n次
X{n,} X,至少n次
X{n,m} X,至少n次,但是不超过m次

System.out.println( "a".matches(".") );
System.out.println( "a".matches("a") );
System.out.println("a".matches("a?") );
System.out.println( "aaa".matches("a*") );
System.out.println( "".matches("a+") );
System.out.println( "aaaaa".matches("a{5}") );
System.out.println( "aaaaaaaaa".matches("a{5,8}") );
System.out.println( "aaa".matches("a{5,}") );
System.out.println( "aaaaab".matches("a{5,}") );

范围表示

[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](减去)

    System.out.println( "a".matches("[a]") );
    System.out.println( "aa".matches("[a]+") );
    System.out.println( "abc".matches("[abc]{3,}") );
    System.out.println( "abc".matches("[abc]+") );
    System.out.println( "dshfshfu1".matches("[^abc]+") );
    System.out.println( "abcdsaA".matches("[a-z]{5,}") );
    System.out.println( "abcdsaA12".matches("[a-zA-Z]{5,}") );
    System.out.println( "abcdsaA12".matches("[a-zA-Z0-9]{5,}") );
    System.out.println( "abdxyz".matches("[a-c[x-z]]+"));
    System.out.println( "bcbcbc".matches("[a-z&&[b-c]]{5,}"));
    System.out.println( "tretrt".matches("[a-z&&[^b-c]]{5,}"));

需求:匹配是否为一个合法的手机号码。

public static void checkTel()                                                   
{                                                                               
String tel = "25800001111";                                                 
String reg = "1[35]\\d{9}";//在字符串中,定义正则出现\ 要一对出现。         
boolean b= tel.matches(reg);                                                
System.out.println(tel+":"+b);                                              
}                                                                               

切割功能

需求1:根据空格对一段字符串进行切割。
public static void splitDemo()                              
    {                                                           
        String str = "aa.bb.cc";                                
        str = "-1     99    4    23";                           
        String[] arr = str.split(" +");                         
        for(String s : arr)                                     
        {                                                       
            System.out.println(s);                              
        }                                                       
    }                                                           
需求2 :根据重叠词进行切割。
public static void splitDemo2()  {                                      
    String str = "sdqqfgkkkhjppppkl";
    String[] arr = str.split("(.)\\1+");
    for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }                          
   }                                      
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容