4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kotlin で文字列から URL を抽出する

Posted at

概要

  • Kotlin の Regex クラスと正規表現で URL の文字列を抜き出す。

サンプルコード

// 文字列からURL文字列を抽出する
fun extractURLs(text: String): List<String> {
    // 正規表現は一例。状況に応じてもっと良いのがあればそれを。
    val regex = "(http://|https://){1}[\\w\\.\\-/:\\#\\?\\=\\&\\;\\%\\~\\+]+"
    val urls = regex.toRegex(RegexOption.IGNORE_CASE).findAll(text).map{it.value}
    return urls.toList()
}

// テストケース
val testCases = listOf(
    "hello, world", // URLが0個
    "goodbye https://example.com", // URLが1個
    "konichiwa https://example.com:80/ http://example.com/foo?q=%E3%81%82#hello", // URLが2個 (ポート番号やパラメータ)
    "sayounara https://example.com/ http://example.com/?a=b&c=d https://example.com/" // URLが3個 (同じURLが2個+1個)
)

// テストケースの文字列からURLを抽出する
for (case in testCases) {
    println("----------------------------------------")
    println("Test case: " + case)
    val urls = extractURLs(case)
    println("Number of matched: " + urls.size)
    for (url in urls) {
        println("URL: " + url)
    }
    println()
}

実行結果

$ kotlinc -script extract_urls.kts 
----------------------------------------
Test case: hello, world
Number of matched: 0

----------------------------------------
Test case: goodbye https://example.com
Number of matched: 1
URL: https://example.com

----------------------------------------
Test case: konichiwa https://example.com:80/ http://example.com/foo?q=%E3%81%82#hello
Number of matched: 2
URL: https://example.com:80/
URL: http://example.com/foo?q=%E3%81%82#hello

----------------------------------------
Test case: sayounara https://example.com/ http://example.com/?a=b&c=d https://example.com/
Number of matched: 3
URL: https://example.com/
URL: http://example.com/?a=b&c=d
URL: https://example.com/

今回の環境

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.14.2
BuildVersion:	18C54

$ kotlinc -version
info: kotlinc-jvm 1.3.11 (JRE 11.0.1+13-LTS)

参考資料

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?