Copyright Jeffrey Friedl http://regex.info/blog/2008-10-16/965