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 6817975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:07:13+00:00 2026-05-26T21:07:13+00:00

I am trying to extract href and src links from an HTML string. According

  • 0

I am trying to extract href and src links from an HTML string. According to this post, I was able to get the image portion. Can anyone help adjust the regular expression to include the href URL in the collection too?

public List<string> GetLinksFromHtml(string content)
{
    string regex = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
    var matches = Regex.Matches(content, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
    var links = new List<string>();

    foreach (Match item in matches)
    {
        string link = item.Groups[1].Value;
        links.Add(link);
    }

    return links;
}
  • 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-26T21:07:14+00:00Added an answer on May 26, 2026 at 9:07 pm

    Okie Doke! Without “an extra library”, and “quick and light”, here ya go:

    <(?<Tag_Name>(a)|img)\b[^>]*?\b(?<URL_Type>(?(1)href|src))\s*=\s*(?:"(?<URL>(?:\\"|[^"])*)"|'(?<URL>(?:\\'|[^'])*)')
    

    or as a C# string:

    @"<(?<Tag_Name>(a)|img)\b[^>]*?\b(?<URL_Type>(?(1)href|src))\s*=\s*(?:""(?<URL>(?:\\""|[^""])*)""|'(?<URL>(?:\\'|[^'])*)')"
    

    This captures the tag name (a or img) into the group “Tag_Name”, the URL type (href or src) into the group “URL_Type”, and the URL into the group “URL” (I know, I got a bit creative with the group names).

    It handles either type of quotes (" or '), and even though any type of quotes in a URL should already be encoded into entities, it will ignore any single-escaped quote characters \' and \".

    It does not ignore unclosed tags (therefore malformed HTML), it will find an opening for one of the tags such as <a or img, then proceed to ignore everything except a greater than (>) up until it finds the matching URL type of attribute (href for a tags and src for img tags), then match the contents. It then quits and does not worry about the rest of the tag!

    Let me know if you’d like me to break it down for you, but here is a sampling of the matches it made for this very page:

    <Match>                                  'Tag' 'URL_Type' 'URL'
    ---------------------------------------- ----- ---------- -----------------------------
    <a href="http://meta.stackoverflow.com"   a     href      http://meta.stackoverflow.com
    <a href="/about"                          a     href      /about
    <a href="/faq"                            a     href      /faq
    <a href="/"                               a     href      /
    <a id="nav-questions" href="/questions"   a     href      /questions
    ...
    <img src="/posts/8066248/ivc/d499"        img   src       /posts/8066248/ivc/d499
    

    It found a total of 140 tags (I am assuming additional posters will increase this somewhat)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract data from this String: Hello there. Blah blahblah blah
I'm trying to extract the image post URLs from a subreddit feed, and render
What I am trying to achieve is to extract all links with a href
I'm trying to extract each a href link on an html page for evaluation
I am trying to extract an article ID from the following href: /MarketUpdate/Pricing/9352730 I
Hi I am trying to extract text which a href defines in a html
I am trying to extract text from XML similar to the following: <p>This is
I am trying to extract publisher information from a string. It comes in various
I am trying to extract the palette from a 5-6-5 (16bit) bitmap image that
I'm trying to extract the first user-right from semicolon separated string which matches a

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.