Kotlin學習筆記(五十八)正規表示式
JavaRegex.java(Java中的正規表示式)
public class JavaRegex {
public static void main(String[] args) {
String source = "Hello,This is my phone number:010-12345678."; //原文字
//正則式,在(三位數-八位數)的格式前後可以有任意文字
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)); //列印匹配到的資訊
}
}
}
![5077812-f904cf4b0dcd7817.jpg](https://upload-images.jianshu.io/upload_images/5077812-f904cf4b0dcd7817.jpg)
JavaRegex.java執行結果
</br>
KotlinUserJavaRegex.kt(在Kotlin中呼叫java的正規表示式)
fun main(args: Array<String>) {
val source = "Hello,This is my phone number:010-12345678." //原文字
//正則式,在(三位數-八位數)的格式前後可以有任意文字
val pattern = """.*(\d{3}-\d{8}).*"""
//用正則式匹配文字獲取匹配器
val matcher = Pattern.compile(pattern).matcher(source)
while (matcher.find()) { //匹配器進行匹配
println(matcher.group()) //列印元文字資訊
println(matcher.group(1)) //列印匹配到的資訊
}
}
![5077812-688d47be3d9aac3b.jpg](https://upload-images.jianshu.io/upload_images/5077812-688d47be3d9aac3b.jpg)
KotlinUserJavaRegex.kt執行結果
</br>
KotlinRegex.kt(使用Kotlin中的正規表示式)
fun main(args: Array<String>) {
val source = "Hello,This is my phone number:010-12345678." //原文字
val pattern = """.*(\d{3}-\d{8}).*""" //正則式
//使用kotlin方法列印出匹配出的內容
Regex(pattern).findAll(source).toList().flatMap(MatchResult::groupValues).forEach(::println)
}
![5077812-b798e93da5f957d8.jpg](https://upload-images.jianshu.io/upload_images/5077812-b798e93da5f957d8.jpg)
KotlinRegex.kt執行結果
相關文章
- 正規表示式學習筆記筆記
- Ruby學習筆記-正規表示式筆記
- 正規表示式學習筆記一筆記
- PERL學習筆記---正規表示式筆記
- 正規表示式學習筆記 (轉)筆記
- JavaScript正規表示式學習筆記(一)JavaScript筆記
- Python學習筆記 - 正規表示式Python筆記
- js加固之正規表示式學習筆記JS筆記
- 學習筆記-5.1.正規表示式1筆記
- Python下正規表示式學習筆記Python筆記
- 正規表示式學習筆記(1)-認識正則筆記
- python 中的正規表示式學習筆記Python筆記
- PERL學習筆記---正規表示式的應用筆記
- 正規表示式(筆記)筆記
- Python學習筆記|Python之正規表示式Python筆記
- JavaScript正規表示式學習筆記(二) - 打怪升級JavaScript筆記
- 正規表示式學習
- 正規表示式速查筆記筆記
- 正規表示式筆記(四)筆記
- 正規表示式筆記(三)筆記
- 正規表示式筆記(二)筆記
- 正規表示式筆記(一)筆記
- python爬蟲學習筆記4-正規表示式Python爬蟲筆記
- Day1-學習筆記-js之正規表示式筆記JS
- 正規表示式的學習
- Oracle正規表示式學習Oracle
- 學習Java:正規表示式Java
- Go 正規表示式學習Go
- JS筆記(15): 正規表示式JS筆記
- 7,正規表示式(perl筆記)筆記
- 正規表示式學習和練習
- 正規表示式入門學習
- 如何快速學習正規表示式
- 正規表示式學習總結
- JavaScript學習1:正規表示式JavaScript
- JavaScript正規表示式迷你書-筆記JavaScript筆記
- python筆記(2) 正規表示式Python筆記
- 《正規表示式必知必會》學習筆記以及示例文件筆記