Created
April 23, 2019 01:07
-
-
Save BoxResin/909909d065defad2789e4b243b25cc46 to your computer and use it in GitHub Desktop.
* 만 인식하는 문자열 매칭
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnyTest { | |
@Test | |
fun test() { | |
"스파게티".matches("*스파게티").let { println(it) } | |
"스파게티".matches("스파게티").let { println(it) } | |
"소스파게티".matches("*스파게티").let { println(it) } | |
"소스파게티".matches("스파게티*").let { println(it) } | |
"스파게티테".matches("*스파게티").let { println(it) } | |
} | |
fun String.matches(pattern: String): Boolean { | |
val target = "^$this$" | |
val tokens: List<String> = "^$pattern$".split('*') | |
.filter { it.isNotEmpty() } | |
var index = 0 | |
for (token: String in tokens) { | |
index = target.indexOf(token, startIndex = index) | |
// 매칭 실패 | |
if (index == -1) { | |
return false | |
} | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment