You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
The text was updated successfully, but these errors were encountered:
NameOfTheDragon
changed the title
Feature request: Named Captured in Regular Expressions
Feature request: Named Captures in Regular Expressions
Jul 6, 2015
Please support named captures in regular expressions.
This expression:
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
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):
The text was updated successfully, but these errors were encountered: