title: C# regex to get video id from youtube and vimeo by url
body: <p>I'm busy trying to create two regular expressions to filter the id from youtube and vimeo video's. I've already got the following expressions;</p>
  
  <pre><code>YouTube: (youtube\.com/)(.*)v=([a-zA-Z0-9-_]+)
  Vimeo: vimeo\.com/([0-9]+)$
  </code></pre>
  
  <p>As i explained below there are  2 types of urls matched by the regular expressions i already created. Several other types of urls from Vimeo and YouTube aren't coverd by the expressions. What i prefer most is that all this can be covered in two expressions. One for all Vimeo video's and one for all youtube video's. I've been busy experimenting with some different expressions, but no succes so far. I'm still trying to master regular expressions, so i hope i'm on the right way and somebody can help me out! If more information is required, please let me know!</p>
  
  <p>VIMEO URLs NOT MATCHED:</p>
  
  <pre><code>http://vimeo.com/channels/hd#11384488
  http://vimeo.com/groups/brooklynbands/videos/7906210
  http://vimeo.com/staffpicks#13561592
  </code></pre>
  
  <p>YOUTUBE URLs NOT MATCHED</p>
  
  <pre><code>http://www.youtube.com/user/username#p/a/u/1/bpJQZm_hkTE
  http://www.youtube.com/v/bpJQZm_hkTE
  http://youtu.be/bpJQZm_hkTE
  </code></pre>
  
  <p>URLs Matched</p>
  
  <pre><code>http://www.youtube.com/watch?v=bWTyFIYPtYU&amp;feature=popular
  http://vimeo.com/834881
  </code></pre>
  
  <p>The idea is to match all the url's mentioned above with two regular expressions. One for vimeo and one for youtube.</p>
  
  <p>UPDATE AFTER ANSWER Sedith:</p>
  
  <p>This is how my expressions look now</p>
  
  <pre><code>public static readonly Regex VimeoVideoRegex = new Regex(@"vimeo\.com/(?:.*#|.*/videos/)?([0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
  public static readonly Regex YoutubeVideoRegex = new Regex(@"youtu(?:\.be|be\.com)/(?:(.*)v(/|=)|(.*/)?)([a-zA-Z0-9-_]+)", RegexOptions.IgnoreCase);
  </code></pre>
  
  <p>And in code i have</p>
  
  <pre><code>var youtubeMatch = url.match(YoutubeVideoRegex );
  var vimeoMatch = url.match(VimeoVideoRegex );
  
  var youtubeIndex = (youtubeMatch.length - 1)
  var youtubeId = youtubeMatch[youtubeIndex];
  </code></pre>
  
  <p>As you can see i now need to find the index where the videoId is in the array with matches returned from the regex. But i want it to only return the id itselfs, so i don't need to modify the code when youtube of vimeo ever decide to change there urls. Any tips on this?</p>