How to match any single character in a string using regular expression with Scala

1 Answer

0 votes
object SingleCharacterRegularExpression {
  def main(args: Array[String]): Unit = {
    val pattern = "b.d".r

    val testStrings = Seq(
      "bud",  // returns true (1)
      "bid",  // returns true (1)
      "bed",  // returns true (1)
      "b d",  // returns true (1)
      "bat",  // returns false (0)
      "bd",   // returns false (0)
      "bead"  // returns false (0)
    )

    testStrings.foreach { testString =>
      val matchFound = pattern.findFirstIn(testString).isDefined
      println(matchFound)
    }
  }
}




/*
run:

true
true
true
true
false
false
false
 
*/

 



answered Feb 15, 2025 by avibootz
...