【45】kotlin 正则
Kotlin 正则 45
2023-09-27 14:27:38 时间
java正则
package com.yzdzy.kotlin.chapter8.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainJava {
public static void main(String... args){
String source = "Hello ,this muy phone number:010-123456789";
String pattern =".*(\\d{3}-\\d{8}).*";
Matcher matcher= Pattern.compile(pattern).matcher(source);
while (matcher.find()){
System.out.println(matcher.group());
System.out.println(matcher.group(1));
}
}
}
kt 同样支持java 但是也有自己的正则方法
package com.yzdzy.kotlin.chapter8.regex
import java.util.regex.Pattern
fun main(args: Array<String>) {
val source = "Hello ,this muy phone number:010-123456789"
val pattern = ".*(\\d{3}-\\d{8}).*"
val matcher = Pattern.compile(pattern).matcher(source)
while (matcher.find()) {
println(matcher.group())
println(matcher.group(1))
}
ktRegex()
}
fun ktRegex(){
val source = "Hello ,this muy phone number:010-123456789"
val pattern = """.*(\d{3}-\d{8}).*"""
Regex(pattern).findAll(source).toList().flatMap (MatchResult::groupValues).forEach(::println)
}
输入
Hello ,this muy phone number:010-123456789
010-12345678
Hello ,this muy phone number:010-123456789
010-12345678
kt 可以使用 """RawString"""
用Raw字符串定义正则表达式
相关文章
- 用十种编程语言开发计算器应用-第四篇-Kotlin
- 安卓Toolbar使用 Demo(Kotlin)
- objectbox入门demo(Kotlin版本)
- Android:kotlin中Gson与JSON的泛型映射解析解决方案
- Kotlin协程协程作用域,CoroutineScope MainScope GlobalScope viewModelScope lifecycleScope 分别代表什么
- Android Jetpack Kotlin/Java pageing3的基础使用。
- Kotlin 判断泛型类型 reified
- Warning: The ‘kotlin-android-extensions‘ Gradle plugin is deprecated. Please use this migration【已解决】
- Kotlin also let 内联扩展函数
- Kotlin: 小菜一碟