How to

search text with named groups using regular expressions

Published: 24. October 2011 | Updated: 26. October 2011
License: Microsoft Public License (MS-PL)
Categories: Framework » Regex
Tags: C# Regex Text
Was this snippet helpful for you? YESYES / NONO

Import namespace

using System.Text.RegularExpressions;

Code

string input = "abcd4987xyz";
string pattern = "(?<number>[0-9]+)";
Match match = Regex.Match(input, pattern);

if (match.Success)
{
    string result = match.Groups["number"].Value;
    Console.WriteLine(result);
}
Console Output:
4987
Send us feedback about this snippet »



Related Snippets: