therealbluepandabear
03/16/2021, 2:39 AMprivate fun extractCommand(command: String): Boolean {
val asArr = command.split("\\s")
if (asArr[0].equals(".attack", ignoreCase = true) && asArr.size == 3)
return true
}
Is there a way to do this with regex? I think that's the best wayturansky
03/16/2021, 2:45 AMnkiesel
03/16/2021, 3:24 AMprivate fun extractCommand(command: String) = Regex("""(?i)\.attack\s+\S+\s+\S+""").matches(command)
caveat: I might misunderstand your goal (why is it call "extract" is it does not return an extracted part?)therealbluepandabear
03/16/2021, 3:25 AMephemient
03/16/2021, 5:48 AMephemient
03/16/2021, 5:49 AMephemient
03/16/2021, 5:51 AMval pattern = """(\S+)\s+(\S+)\s+(\S+)""".toRegex()
fun extractAttack(command: String): Boolean {
val (part1, part2, part3) = pattern.matchEntire(command)?.destructured
?: return false
return part1 == ".attack"
}
ephemient
03/16/2021, 5:52 AMtherealbluepandabear
03/17/2021, 12:52 AM