<p>How do you retrieve selected text using Regex in C#?</p>

<p>I am looking for C# code that is equivalent to this Perl code:</p>

<pre><code>$indexVal = 0;
if($string =~ /Index: (\d*)/){$indexVal = $1;}
</code></pre>

[{'answer_id': 9306, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 4, 'selected': True, 'text': '<pre><code>int indexVal = 0;\nRegex re = new Regex(@"Index: (\\d*)")\nMatch m = re.Match(s)\n\nif(m.Success)\n  indexVal = int.TryParse(m.Groups[1].toString());\n</code></pre>\n\n<p>I might  have the group number wrong, but you should be able to figure it out from here.</p>\n'}, {'answer_id': 9307, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 1, 'selected': False, 'text': "<p>You'll want to utilize the matched groups, so something like...</p>\n\n<pre><code>Regex MagicRegex = new Regex(RegexExpressionString);\nMatch RegexMatch;\nstring CapturedResults;\n\nRegexMatch = MagicRegex.Match(SourceDataString);\nCapturedResults = RegexMatch.Groups[1].Value;\n</code></pre>\n"}, {'answer_id': 9318, 'author': 'Jeff Atwood', 'author_id': 1, 'author_profile': 'https://Stackoverflow.com/users/1', 'pm_score': 3, 'selected': False, 'text': '<p>I think Patrick nailed this one -- my only suggestion is to remember that named regex groups exist, too, so you don\'t <em>have</em> to use array index numbers.</p>\n\n<pre><code>Regex.Match(s, @"Index (?&lt;num&gt;\\d*)").Groups["num"].Value\n</code></pre>\n\n<p>I find the regex is a bit more readable this way as well, though <a href="http://www.codinghorror.com/blog/archives/001016.html" rel="noreferrer">opinions vary</a>...</p>\n'}, {'answer_id': 9322, 'author': 'andynil', 'author_id': 446, 'author_profile': 'https://Stackoverflow.com/users/446', 'pm_score': 1, 'selected': False, 'text': '<p>That would be</p>\n\n<pre><code>int indexVal = 0;\nRegex re = new Regex(@"Index: (\\d*)");\nMatch m = re.Match(s);\n\nif (m.Success)\n    indexVal = m.Groups[1].Index;\n</code></pre>\n\n<p>You can also name you group (here I\'ve also skipped compilation of the regexp)</p>\n\n<pre><code>int indexVal = 0;\nMatch m2 = Regex.Match(s, @"Index: (?&lt;myIndex&gt;\\d*)");\n\nif (m2.Success)\n    indexVal = m2.Groups["myIndex"].Index;\n</code></pre>\n'}, {'answer_id': 11302661, 'author': 'burning_LEGION', 'author_id': 1477076, 'author_profile': 'https://Stackoverflow.com/users/1477076', 'pm_score': 0, 'selected': False, 'text': '<pre><code>int indexVal = 0;\nRegex re = new Regex.Match(s, @"(&lt;?=Index: )(\\d*)");\n\nif(re.Success)\n{\n  indexVal = Convert.ToInt32(re.Value);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/9303', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/889/']