Skip to content

Feature request: Named Captures in Regular Expressions #1

@NameOfTheDragon

Description

@NameOfTheDragon

Please support named captures in regular expressions.

This expression:

const string CommandRegex = @"<(?<Address>(\w\d)),(?<Transaction>\d+),(?<Verb>[A-Za-z]\w+)(=((?<NumericPayload>\d+)|(?<TextPayload>.+)))?>";
const RegexOptions options = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.Singleline;
static readonly Regex Parser = new Regex(CommandRegex, options);

Should produce named captures named "Address", "Transaction", "Verb", "NumericPayload" and "TextPayload" with the last two being optional and mutually exclusive. This expression compiles correctly and works in "desktop" applications, but in .NET Micro Framework it throws

An unhandled exception of type 'System.Text.RegularExpressions.RegexpSyntaxException' occurred in System.Text.RegularExpressions.dll
Additional information: Syntax error: Missing operand to closure

This is a lamentable omission because Named Captures are one of the features that make regular expressions more bearable and lead to much cleaner code. Named captures also have great tooling support in products such as ReSharper.

The following test code should be able to match the example inputs shown in the comment block and retrieve the named captures (again this works correctly on "desktop" apps and even compiles in .netMF):

        /*
         * Commands are understood in one of three basic formats:
         * <F1,234,Nickname=Fred>   -- non-numeric payload
         * <F5,999,MoveTo=1000>     -- numeric payload
         * <F3,0,Center>            -- verb only (no payload)
         * If the RegEx match was successful, then we must have one of these three possibilities.
         * The address, transaction ID and command verb must always be present so we get those first.
         */
        var deviceAddress = matches.Groups["Address"].Value;
        var transaction = int.Parse(matches.Groups["Transaction"].Value);
        var verb = matches.Groups["Verb"].Value;

        // Now we must decide which of the three basic forms we are dealing with.
        // For the verb-only case, we simply set some defaults before checking for a payload.
        var position = Command.NoPosition;
        var data = string.Empty;

        if (matches.Groups["Position"].Success)
            {
            // We have a numeric payload
            position = int.Parse(matches.Groups["NumericPayload"].Value);
            }
        if (matches.Groups["Data"].Success)
            {
            // We have a non-numeric payload
            data = matches.Groups["TextPayload"].Value;
            }
        var source = matches.Value;
        /*
         * Commands are understood in one of three basic formats:
         * <F1,234,Nickname=Fred>   -- non-numeric payload
         * <F5,999,MoveTo=1000>     -- numeric payload
         * <F3,0,Center>            -- verb only (no payload)
         * If the RegEx match was successful, then we must have one of these three possibilities.
         * The address, transaction ID and command verb must always be present so we get those first.
         */
        var deviceAddress = matches.Groups["Address"].Value;
        var transaction = int.Parse(matches.Groups["Transaction"].Value);
        var verb = matches.Groups["Verb"].Value;

        // Now we must decide which of the three basic forms we are dealing with.
        // For the verb-only case, we simply set some defaults before checking for a payload.
        var position = Command.NoPosition;
        var data = string.Empty;

        if (matches.Groups["Position"].Success)
            {
            // We have a numeric payload
            position = int.Parse(matches.Groups["NumericPayload"].Value);
            }
        if (matches.Groups["Data"].Success)
            {
            // We have a non-numeric payload
            data = matches.Groups["TextPayload"].Value;
            }
        var source = matches.Value;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions