Dela via


NETMF 4.2 – Regular Expressions

With the 4.2 release just around the corner, I wanted to highlight another contributions from the community – specifically in this case, Regular Expressions. This feature was contributed by Julius Friedman. 

Regular Expressions

This is desktop compatible although some features like Capture have a more limited scope (i.e. not all the methods are there).  A typical user scenario might be parsing a stream from a serial port like a wireless modem.

This implementation is derived from the Jakarta Apache Library for regular expressions, originally written in Java.  Any ‘missing features’ can be completed over the next few months with your help and feedback.

The code is available in the community branch of the CodePlex branch of the community distribution and can be found under the System Assemblies directory (%SPOCLIENT%\Framework\Core\System\RegularExpressions).  Test Cases are also provided under the test directory. 

Here is a simple example of the Regular Expression taken from our automated tests.

public MFTestResults RegExpTest_3_Substring_Test_0()
{
    bool testResult = false;

    String expected, actual;

    Regex regex;

    string message;

    try
    {
        Log.Comment("Test subst()");
        regex = new Regex("a*b");
        expected = "-foo-garply-wacky-";
        actual = regex.Replace("aaaabfooaaabgarplyaaabwackyb", "-");
        message = "Wrong result of substitution in\"a*b\"";
        testResult = TestTestsHelper.AssertEquals(ref message, ref expected, ref actual);

        Log.Comment("Test subst() with backreferences");
        regex = new Regex("https://[.\\w?/~_@&=%]+");
        expected = "visit us: 1234<a href=\"https://www.apache.org\">https://www.apache.org</a>!";
        actual = regex.Replace("visit us: https://www.apache.org!", "1234<a href=\"$0\">$0</a>");
        message = "Wrong subst() result";
        testResult = TestTestsHelper.AssertEquals(ref message, ref expected, ref actual);

Thanks Julius!!

Technorati Tags: NETMF,.NET Micro Framework,Version 4.2,Regular Expression,Open SOurce Community

Comments

  • Anonymous
    July 06, 2015
    Named captures don't seem to work ("Syntax error: Missing operand to closure") which is a shame, because it is one of the features that makes using RegEx's a lot more bearable. It seems a strange omission when the Match class has the string indexer for retrieving named matches.