Skip to content

Instantly share code, notes, and snippets.

@BoxResin
Created April 23, 2019 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoxResin/909909d065defad2789e4b243b25cc46 to your computer and use it in GitHub Desktop.
Save BoxResin/909909d065defad2789e4b243b25cc46 to your computer and use it in GitHub Desktop.
* 만 인식하는 문자열 매칭
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