Accepted answer

Assuming val txt = txt1 + txt2 + txt3, you can wrap the text into an xml element as a string then parse it as XML and use the xml standard library to extract the anchors.

// can do other cleanup if necessary here such as changing "link!"
def normalize(t: String) = t.toLowerCase()

val txtAsXML = xml.XML.loadString("<root>" + txt + "</root>")
val anchors = txtAsXML \\ "a"
// returns scala.xml.NodeSeq containing the <a> tags

Then you just need to post process until you have the data organized like you want:

val tuples = => normalize(a.text) -> a.attributes("href").toString)
// Seq[String, String] containing elements
// like "mp3" ->

val byTypes = tuples.groupBy(_._1).mapValues(seq =>
// here grouped by types:
// Map(img -> List(, 
//     link! -> List(,
//     mp3 -> List(

Related Query

More Query from same tag