How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in Scala

1 Answer

0 votes
object RegexMatch {
  def main(args: Array[String]): Unit = {
    // Declare the regex pattern
    val pattern = "^htt+p$".r

    // Test strings
    val testStrings = List("http", "htttp", "httttp", "httpp", "htp")

    // Check matches
    for (test <- testStrings) {
      val matches = pattern.matches(test)
      println(s"""Matches "$test": $matches""")
    }
  }
}

 
// Matches "httpp": True or false, depending on how matches() method works
 
 
/*
run:
 
Matches "http": true
Matches "htttp": true
Matches "httttp": true
Matches "httpp": false
Matches "htp": false
 
*/

 



answered May 16 by avibootz
...