/**正则表达式
 *
 */
fun main() {
    var regex = Regex(pattern = "c|d")
    regex.containsMatchIn(input = "abc").apply(::println)       //部分匹配，true
    regex = Regex(pattern = """\d+""")
    regex.matches(input = "50").apply(::println)             //全部匹配，true
    regex.matches(input = "50 dollars").apply(::println)     //全部匹配，false
    regex.matchEntire("100")?.value.apply(::println)   //全部匹配，100
    regex.matchEntire("100 dollars")?.value.apply(::println) //全部匹配，null
    regex.find("100 dollars 22")?.value.apply(::println) //部分匹配，100
    regex.findAll("100 dollars 22").apply {printMatchResult(this,0) } //部分匹配，100---22
    regex.split("ab12cd34ef").apply(::println)  //分割：[ab, cd, ef]
    regex.replace("ab12cd34ef", "x").apply(::println) //替换：abxcdxef
    regex.replace("123-4567-89"){it.groupValues[0].reversed()}.apply(::println)  //321-7654-98

    var inputString = "<span class=\"read-count\">阅读数：641</span>"
    var reg="\\d+(?=</span>)"                             //正向先行断言(?=xxx)
    processReg(reg,inputString,0)               //641
    reg="(?<=<span class=\"read-count\">阅读数：)\\d+"     //正向后行断言(?<=xxx)
    processReg(reg,inputString,0)               //641

    inputString= "020-85653333"
    reg = "(0\\d{2})-(\\d{8})"                  //编号捕获组
    processReg(reg,inputString,2)      //[MatchGroup(value=020-85653333, range=0..11), MatchGroup(value=020, range=0..2), MatchGroup(value=85653333, range=4..11)]
    reg = "(?<quhao>0\\d{2})-(?<haoma>\\d{8})"  //命名捕获组
    processReg(reg,inputString,2)      //[MatchGroup(value=020-85653333, range=0..11), MatchGroup(value=020, range=0..2), MatchGroup(value=85653333, range=4..11)]
    reg="(?:0\\d{2})-(\\d{8})"                  //非捕获组
    processReg(reg,inputString,2)      //[MatchGroup(value=020-85653333, range=0..11), MatchGroup(value=85653333, range=4..11)]

     reg="""(\w)\1+"""      //数字编号组反向引用
     inputString = "aabbbdgbddesddfiiiid"
     processReg(reg,inputString,0)     //aa---bbb---dd---dd---iiii

     processReg(reg,inputString,2)
     reg="""(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)"""
     inputString="http://www.runoob.com:80/html/html-tutorial.html"
     processReg(reg,inputString,1)

    reg="\\d{3,6}"
    inputString="61762828 176 2991 871"          //贪婪模式,匹配到最大能匹配的字符
    processReg(reg,inputString,0)       //617628---176---2991---871
    reg="(\\d{1,2})(\\d{3,4})"
    inputString="61762828 176 2991 87321"        //从左到右的每一个贪婪量词，优先最大数量的满足，剩余再分配下一个量词匹配
    processReg(reg,inputString,0)       //617628---2991---87321
    reg="(\\d{1,2}?)(\\d{3,4})"                  //前一组是懒惰模式，当正则表达式中包含能接受重复的限定符时，通常的行为是匹配尽可能少的字符
    processReg(reg,inputString,0)       //61762---2991---87321
}

fun processReg(pattern:String,input:String,resultType: Int){
   (Regex(pattern).findAll(input)).apply { printMatchResult(this, resultType)}
}

fun printMatchResult(results: Sequence<MatchResult>, resultType: Int) {
    results.forEachIndexed { idx, result ->
        print(
            when (resultType) {
                0 -> result.value
                1 -> result.groupValues
                else -> result.groups
            }
        )
        if (idx < results.count() - 1) print("---")
        else print("\n")
    }
}