I want to extract the http link from inside the anchor tags? The extension that should be extracted should be WMV files only.
Asked
Active
Viewed 3,194 times
1
-
Do you have an example of what you are trying to match? – Jeremy E Jun 08 '09 at 17:04
-
I am trying to match the following: listbox selection video I need a regular expression that should give me: http://www.highoncoding.com/videos/ListBoxSelection.wmv Thanks, – azamsharp Jun 08 '09 at 17:05
3 Answers
2
Because HTML's syntactic rules are so loose, it's pretty difficult to do with any reliability (unless, say, you know for absolute certain that all your tags will use double quotes around their attribute values). Here's some fairly general regex-based code for the purpose:
function extract_urls($html) {
$html = preg_replace('<!--.*?-->', '', $html);
preg_match_all('/<a\s+[^>]*href="([^"]+)"[^>]*>/is', $html, $matches);
foreach($matches[1] as $url) {
$url = str_replace('&', '&', trim($url));
if(preg_match('/\.wmv\b/i', $url) && !in_array($url, $urls))
$urls[] = $url;
}
preg_match_all('/<a\s+[^>]*href=\'([^\']+)\'[^>]*>/is', $html, $matches);
foreach($matches[1] as $url) {
$url = str_replace('&', '&', trim($url));
if(preg_match('/\.wmv\b/i', $url) && !in_array($url, $urls))
$urls[] = $url;
}
preg_match_all('/<a\s+[^>]*href=([^"\'][^> ]*)[^>]*>/is', $html, $matches);
foreach($matches[1] as $url) {
$url = str_replace('&', '&', trim($url));
if(preg_match('/\.wmv\b/i', $url) && !in_array($url, $urls))
$urls[] = $url;
}
return $urls;
}

chaos
- 122,029
- 33
- 303
- 309
1
I wouldn't do this with regex - I would probably use jQuery:
jQuery('a[href$=.wmv]').attr('href')
Compare this to chaos's simplified regex example, which (as stated) doesn't deal with fussy/complex markup, and you'll hopefully understand why a DOM parser is better than a regex for this type of problem.

Peter Boughton
- 110,170
- 32
- 120
- 176
1
Regex:
<a\\s*href\\s*=\\s*(?:(\"|\')(?<link>[^\"]*.wmv)(\"|\'))\\s*>(?<name>.*)\\s*</a>
[Note: \s* is used in several places to match the extra white space characters that can occur in the html.]
Sample C# code:
/// <summary>
/// Assigns proper values to link and name, if the htmlId matches the pattern
/// Matches only for .wmv files
/// </summary>
/// <returns>true if success, false otherwise</returns>
public static bool TryGetHrefDetailsWMV(string htmlATag, out string wmvLink, out string name)
{
wmvLink = null;
name = null;
string pattern = "<a\\s*href\\s*=\\s*(?:(\"|\')(?<link>[^\"]*.wmv)(\"|\'))\\s*>(?<name>.*)\\s*</a>";
if (Regex.IsMatch(htmlATag, pattern))
{
Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
wmvLink = r.Match(htmlATag).Result("${link}");
name = r.Match(htmlATag).Result("${name}");
return true;
}
else
return false;
}
MyRegEx.TryGetHrefDetailsWMV("<td><a href='/path/to/file'>Name of File</a></td>",
out wmvLink, out name); // No match
MyRegEx.TryGetHrefDetailsWMV("<td><a href='/path/to/file.wmv'>Name of File</a></td>",
out wmvLink, out name); // Match
MyRegEx.TryGetHrefDetailsWMV("<td><a href='/path/to/file.wmv' >Name of File</a></td>", out wmvLink, out name); // Match

Rashmi Pandit
- 23,230
- 17
- 71
- 111