正则表达式(Kotlin)
课题
- 使用正则表达式匹配字符串
使用正则表达式 "\d{3}-(\d{4})-\d{2}" 匹配字符串 "123-4567-89"
返回匹配结果:’"123-4567-89" 以及 "4567" - 使用正则表达式替换字符串(模式)
使用正则表达式 "(\d+)-(\d+)-(\d+)" 匹配字符串 "123-4567-89"
使用模式字符串 "$3-$1-$2" 替换匹配结果,返回结果 "89-123-4567"。 - 使用正则表达式替换字符串(回调)
使用正则表达式 "\d+" 匹配字符串 "123-4567-89"
将匹配结果即三个数字串全部翻转过来,返回结果 "321-7654-98"。 - 使用正则表达式分割字符串
使用正则表达式 "%(begin|next|end)%" 分割字符串"%begin%hello%next%world%end%"
返回正则表达式分隔符之间的两个字符串 "hello" 和 "world"。
Kotlin
fun main(args: Array<String>) {
val s = "123-4567-89,987-6543-21"
val r = Regex("""\d{3}-(\d{4})-\d{2}""")
val ms = r.findAll(s)
for ((i, m) in ms.withIndex())
for ((j, v) in m.groupValues.withIndex())
println("group $i,$j : $v")
val r2 = Regex("""(\d+)-(\d+)-(\d+)""")
println(r2.replace(s, "\$3-\$1-\$2"))
val r3 = Regex("""\d+""")
val s3 = r3.replace(s) {
it.groupValues[0].reversed()
}
println(s3)
val r4 = Regex("%(begin|next|end)%")
val s4 = "%begin%hello%next%world%end%"
println(r4.split(s4))
}
/*
group 0,0 : 123-4567-89
group 0,1 : 4567
group 1,0 : 987-6543-21
group 1,1 : 6543
89-123-4567,21-987-6543
321-7654-98,789-3456-12
[, hello, world, ]
*/
正则表达式(Kotlin)的更多相关文章
- Kotlin基础(二)函数的定义与调用
函数的定义与调用 一.预备知识:在Kotlin中创建集合 fun main(args: Array<String>) { //Kotlin中定义各自集合 val ,,,) val list ...
- 用Kotlin破解Android版微信小游戏-跳一跳
前言 微信又更新了,从更新日志上来看,似乎只是一次不痛不痒的小更新.不过,很快就有人发现,原来微信这次搞了个大动作——在小程序里加入了小游戏.今天也是朋友圈被刷爆的缘故. 看到网上 有人弄了一个破解版 ...
- kotlin string
Kotlin String split 操作实践 内容 此文章展示kotlin中对String字符串的split操作,如果你有遇到这方面的需求,希望对你有用. 1. split + 正则 先看下系 ...
- 【Try Kotlin】Kotlin Koans 代码笔记
Kotlin Koans 心印 Introduction 1.Hello, world! Simple Functions Take a look at function syntax and mak ...
- Kotlin——初级篇(八):关于字符串(String)常用操作汇总
在前面讲解Kotlin数据类型的时候,提到了字符串类型,当然关于其定义在前面的章节中已经讲解过了.对Kotlin中的数据类型不清楚的同学.请参考Kotlin--初级篇(三):数据类型详解这篇文章. 在 ...
- Android Email check 正则表达式
Android Email check 正则表达式 (?:[-!#-\\'*+\\x2f-9=?A-Z^-~]+(?:\\.[-!#-\\'*+\\x2f-9=?A-Z^-~]+)*|\"( ...
- Kotlin开发springboot项目(三)
Kotlin开发springboot项目(三) 在线工具 https://www.sojson.com IDEA中Kotlin生成可执行文件1,项目使用Gradle构建2,在model的build.g ...
- Kotlin——关于字符串(String)常用操作汇总
在前面讲解Kotlin数据类型的时候,提到了字符串类型,当然关于其定义在前面的章节中已经讲解过了.对Kotlin中的数据类型不清楚的同学.请参考Kotlin——初级篇(三):数据类型详解这篇文章. 在 ...
- Kotlin for Java Developers 学习笔记
Kotlin for Java Developers 学习笔记 ★ Coursera 课程 Kotlin for Java Developers(由 JetBrains 提供)的学习笔记 " ...
随机推荐
- vue 双向数据绑定 Vue事件介绍 以及Vue中的ref获取dom节点
<template> <div id="app"> <h2>{{msg}}</h2> <input type="te ...
- Centos7修改系统时区timezone
第一步:查询服务器时间 [root@localhost ~]# timedatectl Local time: Sat 2018-03-31 01:11:46 UTC Universal time: ...
- delphi中Application.MessageBox函数用法详解
delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...
- linux 批量替换
sed -i "s/新内容/原内容/g" `ls *.html` sed -i "s/新内容/原内容/g/g" `ls *.php` sed -i " ...
- 《Linux 性能及调优指南》1.1 Linux进程管理
https://blog.csdn.net/ljianhui/article/details/46718835 本文为IBM RedBook的Linux Performanceand Tuning G ...
- linux下用命令修改文件内容
修改test_modify.sh中的LICENSE_INFO test_modify.sh #!/bin/bash licenseInfo=LICENSE_INFO licenseProduct=LI ...
- 关于xampp默认安装后mysql/mariadb密码的修改
关于xampp默认按照后mysql/mariadb密码的修改 默认安装的mysql/mariadb 是没有密码的 只能跳过 然后进行修改密码 /opt/lampp/bin/mysqld_safe - ...
- PHP微信关注自动回复文本消息。
服务器配置URL默认接受 $_GET["echostr"] 配置成功. public function GetShow(){ $token = $this->token; / ...
- centos6 安装 docker 问题
参考:https://www.cnblogs.com/cs294639693/p/10164258.html 第一步:删除 参考:https://www.cnblogs.com/liuyanshen ...
- MonGoDB 在linux 上的安装和配置
01: 下载 linux 版本的二进制包 => https://www.mongodb.com/ 02: 解压 => tar -zxf mongodb-linux-x86_64-3.4. ...