常用正则表达式

Kotlin写法

Url判断字符串是否是WebUrl
val webURL = Pattern.compile( StringBuilder()
            .append("((?:(http|https|Http|Https|rtsp|Rtsp):")
            .append("\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)")
            .append("\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_")
            .append("\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?")
            .append("((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}\\.)+") // named host
            .append("(?:") // plus top level domain
            .append("(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])")
            .append("|(?:biz|b[abdefghijmnorstvwyz])")
            .append("|(?:cat|com|coop|c[acdfghiklmnoruvxyz])")
            .append("|d[ejkmoz]")
            .append("|(?:edu|e[cegrstu])")
            .append("|f[ijkmor]")
            .append("|(?:gov|g[abdefghilmnpqrstuwy])")
            .append("|h[kmnrtu]")
            .append("|(?:info|int|i[delmnoqrst])")
            .append("|(?:jobs|j[emop])")
            .append("|k[eghimnrwyz]")
            .append("|l[abcikrstuvy]")
            .append("|(?:mil|mobi|museum|m[acdghklmnopqrstuvwxyz])")
            .append("|(?:name|net|n[acefgilopruz])")
            .append("|(?:org|om)")
            .append("|(?:pro|p[aefghklmnrstwy])")
            .append("|qa")
            .append("|r[eouw]")
            .append("|s[abcdeghijklmnortuvyz]")
            .append("|(?:tel|travel|t[cdfghjklmnoprtvwz])")
            .append("|u[agkmsyz]")
            .append("|v[aceginu]")
            .append("|w[fs]")
            .append("|y[etu]")
            .append("|z[amw]))")
            .append("|(?:(?:25[0-5]|2[0-4]") // or ip address
            .append("[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]")
            .append("|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]")
            .append("[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}")
            .append("|[1-9][0-9]|[0-9])))")
            .append("(?:\\:\\d{1,5})?)") // plus option port number
            .append("(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~") // plus option query params
            .append("\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?")
            .append("(?:\\b|$)").toString()
        )

        val url1 = "www.baidu.com"
        val url2 = "www.baidu.cn"
        val url3 = "www.baidu"
        val url4 = "baidu.com"
        val url5 = "https://www.baidu.com"
        val url6 = "https://www.baidu"
        val url7 = "https://baidu.com"
        val url8 = "192.168.1.0"
        val url9 = "rtsp://192.168.1.1"

        println("url1 ${webURL.matcher(url1).matches()}")
        println("url2 ${webURL.matcher(url2).matches()}")
        println("url3 ${webURL.matcher(url3).matches()}")
        println("url4 ${webURL.matcher(url4).matches()}")
        println("url5 ${webURL.matcher(url5).matches()}")
        println("url6 ${webURL.matcher(url6).matches()}")
        println("url7 ${webURL.matcher(url7).matches()}")
        println("url8 ${webURL.matcher(url8).matches()}")
        println("url9 ${webURL.matcher(url9).matches()}")
#输出
url1 true
url2 true
url3 false
url4 true
url5 true
url6 false
url7 true
url8 true
url9 true

目前开发中只用到这个,后面再补

你可能感兴趣的:(常用正则表达式)