if you want even less code, you can do
class Parser {
fun isInputValid(input: String): Boolean = REGEX_PATTERN.containsMatchIn(input)
}
private val REGEX_PATTERN = """[0-9]+""".toRegex()
and yes, technically you can also drop the ": Boolean" but I like methods explicitly stating their return value 🤷♂️
and the Parser class looks kind of empty, the method isInputValid could be a top level function as well (without being nested in a class), in that case it should have a more descriptive name though.
fun isInputValid(input: String): Boolean =
REGEX_PATTERN.containsMatchIn(input)
private val REGEX_PATTERN =
"""[0-9]+""".toRegex()
Alternatively you use Parser as kind of a namespace and put isInputValid also into the companion class
class Parser {
companion object {
@JvmStatic // not necessary, but an optimization
fun isInputValid(input: String):
Boolean = REGEX_PATTERN.containsMatchIn(input)
@JvmStatic
private val REGEX_PATTERN = """[0-9]+""".toRegex()
}
}