Friday 19 August 2011

Csharp: Regular Expressions Named Captures

Question:
How to use Named Capture Groups with C# Regular Expression class (Regex)?


Answer:
Capture Groups are usually marked with parenthesis '()' in a regular expression.
Named Capture Groups work the same. You'll just put a name mark at the beginning of the capture group.
Like this: (?<lapin>bunny) or (?'lapin'bunny) -- this captures 'bunny' and calls the group 'lapin'.


Here is an example
I want to extract the string after pageName and pageTopic.
In the code we ask the match for a capture group of each known name and get its first capture.

string val = "pageNameQuick_pageTopicSearch";
if (val != null)
{
    // using named capture groups
    Regex reg = new Regex(@"pageName(?<pn>.*)_pageTopic(?<pt>.*)");
    Match match = reg.Match(val);
    if (match != null && match.Success)
    {
        // Safe function to get named captures
        Func<Match, string, string> GetCaptGroupValue = (m, n) => m.Groups[n] != null ?
            (m.Groups[n].Captures[0] != null ? m.Groups[n].Captures[0].Value : "") :
            "";

        string pageName = GetCaptGroupValue(match, "pn");
        string pageTopic = GetCaptGroupValue(match, "pt");

        System.Console.WriteLine("Page name : {0}; page topic: {1}", pageName, pageTopic);
    }
}



This will print: "Page name : Quick; page topic: Search"


No comments: