Skip to content

Instantly share code, notes, and snippets.

@ekeitho
Created July 12, 2018 04:35
Show Gist options
  • Save ekeitho/a59ca90dafd1800f94c542d928f19599 to your computer and use it in GitHub Desktop.
Save ekeitho/a59ca90dafd1800f94c542d928f19599 to your computer and use it in GitHub Desktop.
Regex . *
fun isMatch(word: String, pattern: String): Boolean {
if (pattern.length == 2 && pattern[0] == '.' && pattern[1] == '*') {
// recursively if we ever get to this, we know to return true regardless
return true
}
if (word.isEmpty() && pattern.isEmpty()) {
return true
}
if (word.isEmpty()) {
if (pattern.length > 1 && pattern[1] == '*') {
return isMatch(word, pattern.substring(2, pattern.length))
}
return false
}
if (pattern.isEmpty()) {
return false
}
if (pattern.length > 1 && pattern[1] == '*') {
if (isMatch(word, pattern.substring(2, pattern.length)) == false) {
if (word[0] == pattern[0] || pattern[0] == '.') {
return isMatch(word.substring(1, word.length), pattern)
}
return false
}
return true
}
if (pattern[0] == '.' || pattern[0] == word[0]) {
return isMatch(word.substring(1, word.length), pattern.substring(1, pattern.length))
} else {
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment