netduino / netmf Goto Github PK
View Code? Open in Web Editor NEW.NET Micro Framework for Netduino
License: Other
.NET Micro Framework for Netduino
License: Other
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;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.