Kotlin语言编程Regex正则表达式实例详解

2022-09-03 15:33:04 56 0
魁首哥

目录

  • 前言
  • Regex 构造函数
  • 常用正则表达方法
  • 示例展示
    • 1.containsMatchIn(input: CharSequence) 包含指定字符串
    • 2.matches(input:javascript CharSequence) 匹配字符串
    • 3.find(input: CharSequence, startIndex: Int = 0) 查找字符串,并返回第一次出现
    • 4.findAll(input: CharSequence, startIndex: Int = 0) 查找字符串,返回所有出现的次数
    • 5.replace(input: CharSequence, replacement: String) 替换字符串
  • 总结

    前言

    回想一下,在学Java时接触的正则表达式,其实Kotlin中也是类似。只不过使用Kotlin 的语法来表达,更为简洁。正则(Regex)用于搜索字符串或替换正则表达式对象,需要使用Regex(pattern:String)类。 在Kotlin中Regex 是在 kotlin.text.regex 包。

    Kotlin语言编程Regex正则表达式实例详解

    Regex 构造函数

    构造函数描述
    Regex(pattern: String)给定的字符串模式创建正则式。
    Regex(pattern: String, option: RegexOption)给定的字符串模式创建一个正则式并给出单个选项
    Regex(pattern: String, options: Set<RegexOption>)给定的字符串模式和给定选项集创建正则表达式

    常用正则表达方法

    方法描述
    fun containsMatchIn(input: CharSequence): Boolean包含至少一个输入字符
    fun find(input: CharSequence, startIndex: Int = 0): MatchResult?返回输入字符序列中正则表达式的第一个匹配项,从给定的startIndex开始
    fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult>返回输入字符串中所有出现的正则表达式,从给定的startIndex开始
    fun matchEntire(input: CharSequence): Matchwww.cppcns.comResult?用于匹配模式中的完整输入字符
    fun matches(input: CharSequence): Boolean输入字符序列是否与正则表达式匹配
    fun replace(input: CharSequence, replacement: String): String用给定的替换字符串替换正则表达式的所有输入字符序列

    示例展示

    这里通过调用几个常见正则函数进行几组数据查找,展示常用正则表达式用法:

    1.containsMatchIn(input: CharSequence) 包含指定字符串

    使用场景:判定是否包含某个字符串

    val regex = Regex(pattern = "Kot")
    val matched = regex.containsMatchIn(inplOsTnIcut = "Kotlin")
    运行结果:
    matched = true
    

    2.matches(input: CharSequence) 匹配字符串

    使用场景:匹配目标字符串

    val regex = """a([bc]+)d?""".toRegex()
    val matched1 = regex.matches(input = "xabcdy")
    val matched2 = regex.matches(input = "abcd")
    运行结果:
    matched1 = false
    matched2 = true
    

    3.find(input: CharSequence, startIndex: Int = 0) 查找字符串,并返回第一次出现

    使用场景:返回首次出现指定字符串

    val phoneNum恰卡编程网ber :String? = Regex(pattern = """\d{3}-\d{3}-\d{4}""")
    .find("phone: 123-456-7890, e..")?.value
    结果打印:
    123-456-7890
    

    4.findAll(input: CharSequence, startIndex: Int = 0) 查找字符串,返回所有出现的次数

    使用场景:返回所有情况出现目标字符串

    val foundResults = Regex("""\d+""").findAll("ab12cd34ef 56gh7 8i")
    val result = StringBuilder()
    for (text in foundResults) {
        result.append(text.value + " ")
    }
    运行结果:
    12 34 56 7 8
    

    5.replace(input: CharSequence, replacement: String) 替换字符串

    使用场景:将指定某个字符串替换成目标字符串

    val replaceWith = RelOsTnIcgex("beautiful")
    val resultString = replaceWith.replace("this picture is beautiful","awesome")
    运行结果:
    this picture is awesome
    

    总结

    通过Kotlin中封装好的正则函数表达式,按规定语法形式传入待查字符串数据以及规则就可以很高效获取到目标数据,它最大的功能就是在于此。可以与Java中的正则形式类比,会掌握的更牢固。

    以上就是Kotlin语言编程Regex正则表达式实例详解的详细内容,更多关于Kotlin Regex正则表达式的资料请关注我们其它相关文章!

    收藏
    分享
    海报
    0 条评论
    56
    上一篇:详解Java ThreadPoolExecutor的拒绝策略 下一篇:Java实现一个简单的长轮询的示例代码

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码