Using Regular Expressions I want to extract all links to files or images contained inside some HTML text. Tried several examples but they failed for many reasons (being the main that I'm not skilled at regular expressions :) )
1) First I've tried this:
> Regex("<img[^>]+src=[""']([^""']+)[""']", RegexOptions.Singleline Or
> RegexOptions.IgnoreCase)
(It works OK for images)
2) And then this:
Regex("href=[""']([^""']+)[""']", RegexOptions.Singleline Or RegexOptions.IgnoreCase)
1) extracts all images, it works OK but thats only a partial solution. 2) extracts all href="asdf", but I want to extract only the href pointing to files, I dont want anchors (#middlesection) or .aspx or even url without extensions like href="www.google.com/site"
I want to know how can I extract all files from a given text, being a file any link that ends with a dot and three characters :)
I'm not interested in ".aspx" or ".html", neither in extensionless urls like "id_content=99", nor anchors like "#anchor123".
Is it possible to pack this into one single RegExp? The idea behind all this is that I have to copy every single files referenced in some HTML from one place to another, thus I need an ArrayList containing only the file paths to copy.
Thanks in advance!
Added some sample code just to clarify that is not about "in the wild" html
Giving this code:
<p>This is a paragraph</p>
<br>
<a href="#someplace">Go to someplace</a>
<ul>
<li><p><a href="../files/document.pdf">Important PDF 1</a></p></li>
<li><p><a href="../files/document.xls">Important XLS</a></p></li>
</ul>
<a href="content.aspx?id_content=55">Go to content 55</a>
<br>
<img src="../images/nicelogo.jpg">
I want to get this:
"../files/document.pdf"
"../files/document.xls"
"../images/nicelogo.jpg"
I DONT want to get this:
"#someplace"
"content.aspx?id_content=55"
Thats it, with the reg exp that I have, I get all the links, I ONLY want the ones that represents a file. The HTML is written by hand by me (long story) so there will be no strange double-double quotes or malformed tags or strange chars.
I know its possible to do because its almost done, I just dont know how to tell "give me only the matches that have ".something" at the end being "something" a three chars long string". Am I clear? :)