I’m almost scared to post this. Every time I post a regex example I typically get about 200 comments showing sexier, smaller, faster examples, but at the same time, I like good (and practical) examples like this. This is why regex was built! So - what’s the example? Given a simple URL with a Youtube video ID in it, how do you extract just the ID? Here’s the URL:
Now - you could just treat that as a list and listLast it, but we don’t know if there will ever be any additional URL parameters. What we really want is the value of “V”. Here is the regex I used:
And here is a complete code template:
<cfset u = "http://www.youtube.com/watch?v=f89niPP64Hg>
<cfset videoid = reReplaceNoCase(u, ".*?v=([a-z0-9\-_]+).*","\1")>
Note that this not work with Youtube’s short url version: http://youtu.be/f89niPP64Hg. For that, if I found youtu.be in the URL I’d probably just listLast with / as the delimiter.