import scala.util.matching.Regex
object RegexValidation {
def isValidString(s: String): Boolean = {
val pattern: Regex = "^[A-Za-z0-9_-]*$".r
pattern.matches(s)
}
def main(args: Array[String]): Unit = {
val s1 = "-abc_123-"
println(if (isValidString(s1)) "yes" else "no")
val s2 = "-abc_123-(!)"
println(if (isValidString(s2)) "yes" else "no")
}
}
/*
run:
yes
no
*/