正则表达式学习,简单记录

申明

原理

  • 有人想知道原理?反正我不想知道.

起因

  • 这个是做letcode得算法题目得时候,感觉用正则会很快,所以逼迫自己去看,欸嘿没想到真给我写出来了;
  • 正则确实好用哈

基本模式匹配

  • ^符号:就是匹配一^字符后面开头的字符;
  //表示以 a 开头的字符串
    fun match1(string : String) : Boolean{
        return string.matches("^a.*".toRegex())
    }

    println(test.match1("a is b"))
    println(test.match1("aPP"))
    println(test.match1("a____"))
    println(test.match1(""))
    println(test.match1("ca"))
    
    true
    true
    true
    false
    false
  • $符号:表示以$前面一个字符结尾
 //表示以 s 结尾的字符串
    fun match2(string : String) : Boolean{
        return string.matches(".*s$".toRegex())
    }

    println(test.match2("a is bs"))
    println(test.match2("aPP"))
    println(test.match2("a____s"))

    true
    false
    true

字符簇

  • [] 括号内不相当于一个范围(我的理解哈)
 //表示单个字符匹配[]中的
    fun match3(string : String) : Boolean{
        return string.matches("[132abc]".toRegex())
    }
    println(test.match3("1"))
    println(test.match3("a"))
    println(test.match3("ss"))
    println(test.match3("c"))
    println(test.match3("123a"))

    true
    true
    false
    true
    false
[1234] //匹配1234中的任意一个就行
[a-z] // 匹配所有的小写字母 
[A-Z] // 匹配所有的大写字母 
[a-zA-Z] // 匹配所有的字母 
[0-9] // 匹配所有的数字 
[0-9\.\-] // 匹配所有的数字,句号和减号 
[ \f\r\t\n] // 匹配所有的白字符
  • [^a] 表示排除括号内的字符a匹配其他所有字符,在方括号里面的,表示排除跟着的字符
 //表示单个字符匹配排除[]中的
    fun match4(string : String) : Boolean{
        return string.matches("[^132abc]".toRegex())
    }
    println(test.match4("1"))
    println(test.match4("a"))
    println(test.match4("5"))
    println(test.match4("c"))
    println(test.match4("123a"))

    false
    false
    true
    false
    false
  • 特殊字符 . 和 * 以及+: . 表示匹配任意字符, * 表示匹配他前面的字符0个或者任意个. + 表示匹配一个或者多个前面的字符
  //.表示匹配任意单个字符
    fun match5(string : String) : Boolean{
        return string.matches(".".toRegex())
    }

 //表示匹配0个或者任意a
    fun match6(string : String) : Boolean{
        return string.matches("a*".toRegex())
    }

    //表示匹配1个或者任意a
    fun match7(string : String) : Boolean{
        return string.matches("a+".toRegex())
    }
//例子就不继续写了,反正知道是啥意思就好了
  • PHP的正则表达式有一些内置的通用字符簇,JAVA和Kotlin里面是不是一样的我就不知道了,反正我也不用,要用自己写不就好了
字符簇           描述
[[:alpha:]]     任何字母
[[:digit:]]     任何数字
[[:alnum:]]     任何字母和数字
[[:space:]]     任何空白字符
[[:upper:]]     任何大写字母
[[:lower:]]     任何小写字母
[[:punct:]]     任何标点符号
[[:xdigit:]]    任何16进制的数字,相当于[0-9a-fA-F]

确定重复出现

  • 使用{}表示前面的字符重复出现的次数,{0,1}表示出现0次或者1次;{0,}表示0次或多次= *字符;{1,}一次或多次= +字符
  • 使用(){}表示重复出现的结构
 //表示axxxc的结构。其中xxx可以是任意字符,也可以是空字符""
    fun match9(string : String) : Boolean{
        return string.matches("(a.*c){3}".toRegex())
    }

    println("acacac = ${test.match9("acacac")}")
    println("aaaacaca___c = ${test.match9("aaaacaca___c")}")
    println("bc = ${test.match9("bc")}")
    println("bc = ${test.match9("bc")}")
    println("a_ca_ca_c = ${test.match9("a_ca_ca_c")}")
    println("a_a = ${test.match9("a_a")}")
    println("bbb = ${test.match9("bbb")}")

    //打印结果
    acacac = true
    aaaacaca___c = true
    bc = false
    bc = false
    a_ca_ca_c = true
    a_a = false
    bbb = false

常用的正则

  • 手机号码表达 这个是自己写的比较简单的,其实网上可以找到带佬写的更叼的;
  //手机号码 138-1277-0000
  //1开头,第二个数在3-9中,后面9个数在0-9中
    fun match10(string : String) : Boolean{
        return string.matches("1[3-9][0-9]{9}".toRegex())
    }
  • 省份证匹配;这里用到了\转义符,因为\d表示了所有数字,emmm反正就这么用了。其他特殊字符也是需要用\转义的,比如换行等字符;
  //省份证匹配
    fun match11(string : String) : Boolean{
        //^(\d{6}) 表示6个数字开头
        //(19|2[0-3]) 表示年份前面两个数 19或者 20,21,22,23
        // \\d{2} 表示两为数的年份后两位
        //(0[1-9]|1[0-2]) 表示月份 0开头的有01-09   或者 1开头的10-12
        //(0[1-9]|1[0-9]|2[0-9]|3[0-1]) 表示日期 0开头的01-09 1开头的10-19 2开头20-29 3开头30-31
        //(\d{3})  表示三个数字
        //(\d|X|x)?$  表示以数字或者X或者x结尾 ? 表示这个数也可以没有
        return string.matches("^(\\d{6})(19|2[0-3])\\d{2}(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])(\\d{3})(\\d|X|x)?$".toRegex())
    }

    println("420528199206140000 is id card is ${test.match11("420528199206140000")}")
    println("4205281992061400001 is id card is ${test.match11("4205281992061400001")}")
    println("42052819920614000x is id card is ${test.match11("42052819920614000x")}")
    println("42052819920614000X is id card is ${test.match11("42052819920614000X")}")
    println("420528189206140000 is id card is ${test.match11("420528189206140000")}")
  
    420528199206140000 is id card is true
    4205281992061400001 is id card is false
    42052819920614000x is id card is true
    42052819920614000X is id card is true
    420528189206140000 is id card is false  //这个是因为1892年,那肯定还没...卧槽不一定,131岁,如果还活着应该也是有身份证的。这里最大也就算到从1900年开始,应该没问题;
//菜鸟列举的
^[a-zA-Z0-9_]{1,}$      // 所有包含一个以上的字母、数字或下划线的字符串 
^[1-9][0-9]{0,}$        // 所有的正整数 
^\-{0,1}[0-9]{1,}$      // 所有的整数 
^[-]?[0-9]+\.?[0-9]+$   // 所有的浮点数
[^a-z] //除了小写字母以外的所有字符 
[^\\\/\^] //除了(\)(/)(^)之外的所有字符 
[^\"\'] //除了双引号(")和单引号(')之外的所有字符

总结

  • 以前一直觉得正则好难,其实静下心来认真看,其实也就那样,当然正则肯定不止我说的这么点内容,但是如果只是在项目中使用,也用不着太难的,简单的正则能提高效率,复杂的还不如用别的方法,要不然你去维护看个正则都要分析一万年;
  • 所以静心做事,持之以恒,大部分事情都是很好解决的。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容