Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are 2 approaches using LINQ to XML and regex. Although some people frown upon parsing HTML with regex, this particular case doesn't have nested elements so it is a reasonable solution. LINQ to XML would only work if your HTML is well-formed. Otherwise take a look at the <a href="http://www.codeplex.com/htmlagilitypack" rel="nofollow noreferrer">HTML Agility Pack</a>.</p> <p><strong>EDIT:</strong> for your sample <code>Elements()</code> works with LINQ to XML. However, if you have many nested HTML tags then you may want to use <code>Descendants()</code> to reach all desired tags.</p> <pre><code>string input = @"&lt;p&gt;This is some html text. my favourite website is &lt;a href=""http://www.google.com""&gt;google&lt;/a&gt; and my favourite help site is &lt;a href=""http://www.stackoverflow.com""&gt;stackoverflow&lt;/a&gt; and i check my email at &lt;a href=""http://www.gmail.com""&gt;gmail&lt;/a&gt;. the url to my site is http://www.mysite.com. &lt;img src=""http://www.someserver.com/someimage.jpg"" alt=""""/&gt;&lt;/p&gt;"; var xml = XElement.Parse(input); var result = xml.Elements() .Where(e =&gt; e.Name == "img" || e.Name == "a") .Select(e =&gt; e.Name == "img" ? e.Attribute("src").Value : e.Attribute("href").Value); foreach (string item in result) { Console.WriteLine(item); } string pattern = @"&lt;(?:a|img).+?(?:href|src)=""(?&lt;Url&gt;.+?)"".*?&gt;"; foreach (Match m in Regex.Matches(input, pattern)) { Console.WriteLine(m.Groups["Url"].Value); } </code></pre> <p><strong>EDIT #2:</strong> in response to your update about RegexBuddy, I wanted to point out the tool I use. <a href="http://www.ultrapico.com/Expresso.htm" rel="nofollow noreferrer">Expresso</a> is a good free tool (email registration only, but it's free). The author also wrote <a href="http://www.codeproject.com/KB/dotnet/regextutorial.aspx" rel="nofollow noreferrer">The 30 Minute Regex Tutorial</a> which you can use to follow along and is included in the help file of Expresso.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload