Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Named Captures in Regular Expressions #1

Open
NameOfTheDragon opened this issue Jul 6, 2015 · 1 comment
Open

Comments

@NameOfTheDragon
Copy link

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;
@NameOfTheDragon NameOfTheDragon changed the title Feature request: Named Captured in Regular Expressions Feature request: Named Captures in Regular Expressions Jul 6, 2015
@NameOfTheDragon
Copy link
Author

Cross reference: NETMF/netmf-interpreter#225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant