ColdFusion regex to match valid Youtube links (need improvement)
coldfusion, regex, youtube
Solution
(?<=http:\/\/)(?:www\.)?youtu(?:be\.com\/(?:watch\?|user\/|v\/|embed\/)\S+|\.be\/\S+)
See this demo.
Problem
Although similar questions were asked on here multiple times already, I've got request to amend an existing regex line to improve it. Pretty sure this will help others in the same situation too. What I'm trying to achieve is to match valid YouTube video URLs using ColdFusion regex. Here's what I've currently got: ``` ReMatch('^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^##\&\?]*).*',mylink) ``` This works for the following URL types: ``` http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s http://www.youtube.com/embed/0zM3nApSvMg?rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg http://youtu.be/0zM3nApSvMg ``` However, the following URL for whatever reason is getting matched too: ``` http://www.theguardian.com/media/2013/nov/29/russell-brand-rages-sun-rupert-murdoch ``` How can I amend the code to be a bit more accurate? Maybe making sure that the 'youtu' part is paramount to the link would help as I think the current regex only takes it as one of the optional parts? Trouble is I'm not able to amend this code myself, hence asking for help here. //////EDITED//////////////// Thanks to Omega's answer below, with a little amendment here's the pattern that worked for my case: ``` ReMatch('(http:\/\/)(?:www\.)?youtu(?:be\.com\/(?:watch\?|user\/|v\/|embed\/)\S+|\.be\/\S+)',mylink) ``` Also, it is worth noting I had to strip the lookbehind part from the suggested pattern as ColdFusion does not support it.