Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6697119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:25:20+00:00 2026-05-26T06:25:20+00:00

I’m attempting to extract urls from a string, they aren’t standardized so some are

  • 0

I’m attempting to extract urls from a string, they aren’t standardized so some are within href tags, others on their own.

Also I need them to be sorted by type, so for example the following strings:

var txt1: String = "Some text! <a href="http://www.google.com/test.mp3">MP3</a>"
var txt2: String = "Some text! <a href="http://www.google.com/test.jpg">IMG</a>"
var txt3: String = "Some more! <a href="http://www.google.com/">Link!</a>"

So these strings are all concatenated and contain 3 urls, I’m looking for something along the lines of:

var result: List = List(

    "mp3" -> List("http://www.google.com/test.mp3"),
    "img" -> List("http://www.google.com/test.jpg"),
    "url" -> List("http://www.google.com/")
)

I’ve looked into regex but have only go so far as to extract hrefs without defining types, and this also doesn’t retrieve urls on their own outside of tags

val hrefRegex = new Regex("""\<a.*?href=\"(http:.*?)\".*?\>.*?\</a>""");
val hrefs:List[String]= hrefRegex.findAllIn(txt1.mkString).toList;

Any help is much appreciated, thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:25:21+00:00Added an answer on May 26, 2026 at 6:25 am

    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 = anchors.map(a => normalize(a.text) -> a.attributes("href").toString)
    // Seq[String, String] containing elements
    // like "mp3" -> http://www.google.com/test.mp3
    
    val byTypes = tuples.groupBy(_._1).mapValues(seq => seq.map(_._2))
    // here grouped by types:
    // Map(img -> List(http://www.google.com/test.jpg), 
    //     link! -> List(http://www.google.com/),
    //     mp3 -> List(http://www.google.com/test.mp3))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.