Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing HTML document: Regular expression or LINQ?
    primarykey
    data
    text
    <p>Trying to parse an HTML document and extract some elements (any links to text files).</p> <p>The current strategy is to load an HTML document into a string. Then find all instances of links to text files. It could be any file type, but for this question, it's a text file.</p> <p>The end goal is to have an <code>IEnumerable</code> list of string objects. That part is easy, but parsing the data is the question.</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;title&gt;Blah&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;br/&gt; &lt;div&gt;Here is your first text file: &lt;a href="http://myServer.com/blah.txt"&gt;&lt;/div&gt; &lt;span&gt;Here is your second text file: &lt;a href="http://myServer.com/blarg2.txt"&gt;&lt;/span&gt; &lt;div&gt;Here is your third text file: &lt;a href="http://myServer.com/bat.txt"&gt;&lt;/div&gt; &lt;div&gt;Here is your fourth text file: &lt;a href="http://myServer.com/somefile.txt"&gt;&lt;/div&gt; &lt;div&gt;Thanks for visiting!&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The initial approaches are:</p> <ul> <li>load the string into an XML document, and attack it in a Linq-To-Xml fashion.</li> <li>create a regex, to look for a string starting with <code>href=</code>, and ending with <code>.txt</code></li> </ul> <p>The question being: </p> <ul> <li>what would that regex look like? I am a regex newbie, and this is part of my regex learning. </li> <li>which method would you use to extract a list of tags?</li> <li>which would be the most performant way?</li> <li>which method would be the most readable/maintainable?</li> </ul> <p><hr> <strong>Update:</strong> Kudos to <a href="https://stackoverflow.com/questions/907563/parsing-html-document-regular-expression-or-linq/907571#907571">Matthew</a> on the HTML Agility Pack suggestion. It worked just fine! The XPath suggestion works as well. I wish I could mark both answers as 'The Answer', but I obviously cannot. They are both valid solutions to the problem.</p> <p>Here's a C# console app using the regex suggested by <a href="https://stackoverflow.com/questions/907563/parsing-html-document-regular-expression-or-linq/907639#907639">Jeff</a>. It reads the string fine, and will not include any href that is not ended with .txt. With the given sample, it correctly does NOT include the <code>.txt.snarg</code> file in the results (as provided in the HTML string function).</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.IO; namespace ParsePageLinks { class Program { static void Main(string[] args) { GetAllLinksFromStringByRegex(); } static List&lt;string&gt; GetAllLinksFromStringByRegex() { string myHtmlString = BuildHtmlString(); string txtFileExp = "href=\"([^\\\"]*\\.txt)\""; List&lt;string&gt; foundTextFiles = new List&lt;string&gt;(); MatchCollection textFileLinkMatches = Regex.Matches(myHtmlString, txtFileExp, RegexOptions.IgnoreCase); foreach (Match m in textFileLinkMatches) { foundTextFiles.Add( m.Groups[1].ToString()); // this is your captured group } return files; } static string BuildHtmlString() { return new StringReader(@"&lt;html&gt;&lt;head&gt;&lt;title&gt;Blah&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;br/&gt; &lt;div&gt;Here is your first text file: &lt;a href=""http://myServer.com/blah.txt""&gt;&lt;/div&gt; &lt;span&gt;Here is your second text file: &lt;a href=""http://myServer.com/blarg2.txt""&gt;&lt;/span&gt; &lt;div&gt;Here is your third text file: &lt;a href=""http://myServer.com/bat.txt.snarg""&gt;&lt;/div&gt; &lt;div&gt;Here is your fourth text file: &lt;a href=""http://myServer.com/somefile.txt""&gt;&lt;/div&gt; &lt;div&gt;Thanks for visiting!&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;").ReadToEnd(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    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