You are trying to access Groups[1], but it doesn't look like your regular expression has captured any groups. Can't you just use youtubeMatch.ToString or Groups[0] to get what you're after (the whole match?) instead of trying to access sub-groups that you haven't defined?
EDIT
Your expression seems to work better when I remove the ?:
from the last set of parentheses. My understanding is that if your parenthesized expression starts with ?:
you are explicitly indicating that you don't want the expression to be captured into a group, and if it starts with ?=
you are defining a zero-width assertion, which, because it is zero-width, obviously doesn't capture anything. You need some parentheses in your expression that actually do capture something if you want Groups
to be populated.
EDIT
Based on comments so far and some guessing at what you're trying to do, here is an updated regular expression and some updated code to demonstrate it. It works with the 2 URLs you mentioned in your comment where one would not work:
var re = new System.Text.RegularExpressions.Regex(
@"^http:\/\/(?:www\.)?youtube.com\/watch\?[^?]*v=(\w+)\b[^\s?]*$",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
var match = re.Match(textBox1.Text);
if (match.Success)
{
textBox2.Text = match.Value;
if (match.Groups.Count > 1)
textBox3.Text = match.Groups[1].Value;
else
textBox3.Text = "Group missing";
}
else
{
textBox2.Text = "(No match)";
textBox3.Text = string.Empty;
}
textBox2 is populated with the whole matching URL and textBox3 is populated with just the "v" query parameter.