正则表达式通常是指用于搜索字符串或替换正则表达式对象的正则表达式。要使用它的函数,无涯教程需要使用Regex(pattern:String)类。 Kotlin的Regex类可在kotlin.text.regex包中找到。
Regex(pattern: String) | 根据给定的字符串模式创建一个正则表达式。 |
Regex(pattern: String, option: RegexOption) | 根据给定的字符串模式和给定的单个选项创建一个正则表达式。 |
Regex(pattern: String, options: Set<RegexOption>) | 根据给定的字符串模式和给定的选项集创建一个正则表达式。 |
Functions | Descriptions |
---|---|
fun containsMatchIn(input: CharSequence): Boolean | 它表示正则表达式至少包含一个输入字符 |
fun find( input: CharSequence, startIndex: Int = 0 ): MatchResult? | 从给定的startIndex开始,它返回输入字符序列中正则表达式的第一个匹配项。 |
fun findAll( input: CharSequence, startIndex: Int = 0 ): Sequence<MatchResult> | 从给定的startIndex开始,返回输入字符串中所有出现的正则表达式。 |
funmatchEntire(input: CharSequence): MatchResult? | 用于匹配模式中完整的输入字符。 |
infix fun matches(input: CharSequence): Boolean | 指示所有输入字符序列在正则表达式中是否匹配。 |
fun replace(input: CharSequence, replacement: String): String | 用给定的替换字符串替换所有正则表达式的输入字符序列。 |
fun replaceFirst( input: CharSequence, replacement: String ): String | 用给定的替换字符串替换给定的输入字符串中的第一次出现的正则表达式。 |
fun split(input: CharSequence, limit: Int = 0): List<String> | 分割正则表达式的输入字符序列。 |
fun toPattern(): Pattern fun toString(): String | 以字符串形式返回正则表达式。 |
fun main(args: Array<String>){ val regex = Regex(pattern = "ko") val matched = regex.containsMatchIn(input = "kotlin") println(matched) }
输出:
true
Regex函数的结果基于匹配的正则表达式模式和输入字符串。有些函数检查部分匹配,而有些则检查完整匹配。
fun main(args: Array<String>){ val regex = """a([bc]+)d?""".toRegex() val matched = regex.containsMatchIn(input = "xabcdy") println(matched) }
输出:
true
matchs(input:CharSequence):regex布尔函数会检查所有输入字符序列是否匹配正则表达式。
fun main(args: Array<String>){ val regex = """a([bc]+)d?""".toRegex() val matched1 = regex.matches(input = "xabcdy") val matched2 = regex.matches(input = "xabcdyabcd") val matched3 = regex.matches(input = "abcd") println(matched1) println(matched2) println(matched3) }
输出:
false false true
matchEntire()函数用于匹配模式中的完整输入字符。
fun main(args: Array<String>){ val regex = Regex("abcd") val matchResult1 = regex.matchEntire("abcd")?.value val matchResult2 = regex.matchEntire("abcda")?.value val matchResult3 = Regex("""\d+""").matchEntire("100")?.value val matchResult4 = Regex("""\d+""").matchEntire("100 dollars")?.value println(matchResult1) println(matchResult2) println(matchResult3) println(matchResult4) }
输出:
abcd null 100 null
fun main(args: Array<String>){ val emailParttern = Regex("""\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}""") val email :String? = emailParttern.find("this is my email mymail@google.com")?.value println(email) val phoneNumber :String? = Regex(pattern = """\d{3}-\d{3}-\d{4}""") .find("phone: 123-456-7890, e..")?.value println(phoneNumber) }
输出:
mymail@google.com 123-456-7890
正则表达式的findAll()函数根据提供的模式返回匹配结果的序列。
fun main(args: Array<String>){ val foundResults = Regex("""\d+""").findAll("ab12cd34ef 56gh7 8i") val result = StringBuilder() for (findText in foundResults) { result.append(findText.value + " ") } println(result) }
输出:
12 34 56 7 8
Regex replace()函数用指定的替换字符串替换输入字符序列中的所有匹配模式。
fun main(args: Array<String>){ val replaceWith = Regex("beautiful") val resultString = replaceWith.replace("this picture is beautiful","awesome") println(resultString) }
输出:
this picture is awesome
Regex replaceFirst()函数用指定的替换字符串替换输入字符序列中匹配模式的第一个匹配项。
fun main(args: Array<String>){ val replaceWith = Regex("beautiful") val resultString = replaceWith.replaceFirst("nature is beautiful, beautiful is nature","awesome") println(resultString) }
输出:
nature is awesome, beautiful is nature
regex split()函数根据提供的模式拆分输入字符序列。此拆分值在列表中返回。
fun main(args: Array<String>){ val splitedValue = Regex("""\d+""").split("ab12cd34ef") val nonsplited= Regex("""\d+""").split("nothing match to split" ) println(splitedValue) println(nonsplited) }
输出:
[ab, cd, ef] [nothing match to split]
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕