import scala.util.matching.Regex
object Program {
def extractContentBetweenTags(str: String, tagName: String): Option[String] = {
// Build a regex pattern using the specified tag name
val pattern = s"<$tagName>(.*?)</$tagName>"
val regex = new Regex(pattern)
// Find the first match
regex.findFirstMatchIn(str) match {
case Some(matched) => Some(matched.group(1)) // Return the content inside the tags
case None => None // Return None if no match is found
}
}
def main(args: Array[String]): Unit = {
val str = "abcd <tag>efg hijk lmnop</tag> qrst uvwxyz"
// Call the function to extract the substring
val content = extractContentBetweenTags(str, "tag")
content match {
case Some(value) => println(s"Extracted content: $value")
case None => println("No matching tags found.")
}
}
}
/*
run:
Extracted content: efg hijk lmnop
*/