<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>beer planet &#187; regexp</title> <atom:link href="http://beerpla.net/tag/regexp/feed/" rel="self" type="application/rss+xml" /><link>http://beerpla.net</link> <description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description> <lastBuildDate>Thu, 17 May 2012 22:50:53 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <atom:link rel='hub' href='http://beerpla.net/?pushpress=hub'/> <item><title>Quick Perl Snippet: Finding If A File Has A Media Extension Using Regex</title><link>http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/</link> <comments>http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/#comments</comments> <pubDate>Fri, 21 Mar 2008 21:11:34 +0000</pubDate> <dc:creator>Artem Russakovskii</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[extension]]></category> <category><![CDATA[file]]></category> <category><![CDATA[media]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[regex]]></category> <category><![CDATA[regexp]]></category> <category><![CDATA[type]]></category> <category><![CDATA[url]]></category> <guid
isPermaLink="false">http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/</guid> <description><![CDATA[<p>Sometimes in my line of work, I need to figure out if a url or filename point to a media file by checking for the file extension. If it&#039;s a url, however, it may be followed by various parameters. Not to overcomplicate things, I came up with the following Perl code:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td
class="code"><pre>#!/usr/bin/perl -w
use strict;
my $name = &#34;some_file.flv&#34;; # or http://example.com/file.mp4?foo=bar
my $is_media_type = ($name =~ /\.(wmv&#124;avi&#124;flv&#124;mov&#124;mkv&#124;mp..?&#124;swf&#124;ra.?&#124;rm&#124;as.&#124;m4[av]&#124;smi.?)\b/i);
if($is_media_type){
  print &#34;media extension found\n&#34;;
}
else{
  print &#34;not a media file\n&#34;;
}</pre></td></tr></table></div><p>This gets the job done without triggering any false positives (at least for the files/urls I&#039;ve been dealing with so far). Am I missing any obvious types? Do you ...<div
class=clear></div> <a
href="http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/" class="read_more"><div
class=excerpt-end>Read the rest of this article &#187;</div></a></p>]]></description> <content:encoded><![CDATA[<p>Sometimes in my line of work, I need to figure out if a url or filename point to a media file by checking for the file extension. If it&#039;s a url, however, it may be followed by various parameters. Not to overcomplicate things, I came up with the following Perl code:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td
class="code"><pre>#!/usr/bin/perl -w
use strict;
my $name = &quot;some_file.flv&quot;; # or http://example.com/file.mp4?foo=bar
my $is_media_type = ($name =~ /\.(wmv|avi|flv|mov|mkv|mp..?|swf|ra.?|rm|as.|m4[av]|smi.?)\b/i);
if($is_media_type){
  print &quot;media extension found\n&quot;;
}
else{
  print &quot;not a media file\n&quot;;
}</pre></td></tr></table></div><p>This gets the job done without triggering any false positives (at least for the files/urls I&#039;ve been dealing with so far). Am I missing any obvious types? Do you have a better way to accomplish the same thing? If so, please share in the comments.</p><div
class="shr-bookmarks shr-bookmarks-expand"><ul
class="socials"><li
class="shr-twitter"> <a
href="http://www.shareaholic.com/api/share/?title=Quick+Perl+Snippet%3A+Finding+If+A+File+Has+A+Media+Extension+Using+Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li
class="shr-facebook"> <a
href="http://www.shareaholic.com/api/share/?title=Quick+Perl+Snippet%3A+Finding+If+A+File+Has+A+Media+Extension+Using+Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-googlebuzz"> <a
href="http://www.shareaholic.com/api/share/?title=Quick+Perl+Snippet%3A+Finding+If+A+File+Has+A+Media+Extension+Using+Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a></li><li
class="shr-reddit"> <a
href="http://www.shareaholic.com/api/share/?title=Quick+Perl+Snippet%3A+Finding+If+A+File+Has+A+Media+Extension+Using+Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a></li><li
class="shr-hackernews"> <a
href="http://www.shareaholic.com/api/share/?title=Quick+Perl+Snippet%3A+Finding+If+A+File+Has+A+Media+Extension+Using+Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=202&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a></li><li
class="shr-delicious"> <a
href="http://www.shareaholic.com/api/share/?title=Quick+Perl+Snippet%3A+Finding+If+A+File+Has+A+Media+Extension+Using+Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-stumbleupon"> <a
href="http://www.shareaholic.com/api/share/?title=Quick+Perl+Snippet%3A+Finding+If+A+File+Has+A+Media+Extension+Using+Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="shr-mail"> <a
href="http://www.shareaholic.com/api/share/?title=Quick%20Perl%20Snippet%3A%20Finding%20If%20A%20File%20Has%20A%20Media%20Extension%20Using%20Regex&amp;link=http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/&amp;notes=Sometimes%20in%20my%20line%20of%20work%2C%20I%20need%20to%20figure%20out%20if%20a%20url%20or%20filename%20point%20to%20a%20media%20file%20by%20checking%20for%20the%20file%20extension.%20If%20it%27s%20a%20url%2C%20however%2C%20it%20may%20be%20followed%20by%20various%20parameters.%20Not%20to%20overcomplicate%20things%2C%20I%20came%20up%20with%20the%20following%20Perl%20code%3A%0A%0A%23%21%2Fusr%2Fbin%2Fperl%20-w%0Ause%20strict%3B%0Amy&amp;short_link=http://bit.ly/cMBVAD&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=201&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a></li></ul><div
style="clear: both;"></div></div> Similar Posts:<ul><li><a
href="http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/" rel="bookmark" title="April 8, 2009">[Perl] Finding Files, The Fun And Elegant Way</a></li><li><a
href="http://beerpla.net/2007/08/04/watch-a-useful-linux-command-you-may-have-never-heard-of/" rel="bookmark" title="August 4, 2007">Watch &#8211; A Useful Linux Command You May Have Never Heard Of</a></li><li><a
href="http://beerpla.net/2009/03/17/twitter-autocomplete-auto-url-expansion-auto-url-shortener-auto-pagination-rt-button-nested-replies-inline-media-embed-search-tabs-and-more/" rel="bookmark" title="March 17, 2009">Twitter.com Autocomplete, Auto URL Expansion, Auto URL Shortener, RT Button, Nested Replies, Inline Media Embed, Search Tabs, And More</a></li><li><a
href="http://beerpla.net/2008/04/26/how-to-list-files-within-tgz-targz-archives/" rel="bookmark" title="April 26, 2008">How To List Files Within tgz (tar.gz) Archives</a></li><li><a
href="http://beerpla.net/2010/01/13/wordpress-plugin-development-how-to-include-css-and-javascript-conditionally-and-only-when-needed-by-the-posts/" rel="bookmark" title="January 13, 2010">[WordPress Plugin Development] How To Include CSS and JavaScript Conditionally And Only When Needed By The Posts</a></li></ul><p><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbeerpla.net%2F2008%2F03%2F21%2Fquick-snippet-finding-if-a-file-has-a-media-extension-using-regex%2F&amp;title=Quick%20Perl%20Snippet%3A%20Finding%20If%20A%20File%20Has%20A%20Media%20Extension%20Using%20Regex" id="wpa2a_2"><img
src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
