Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Sunday, August 22, 2010

Rex–Regular Expression Exploration

samplesfa

Rex – is an MSR released tool that generates strings based on a regex expression. It’s a great tool for generating input for unit tests as well as output data for mocks. It is also makes it really easy to determine what that complex regex that you downloaded from the internet does or if the regex that you wrote really works or not.

One thing to know is that when you run the tool and if you see output with “\u”, then the tool is outputting unicode data. In this case, just add the /e:ASCII option to the command line to get plan text output.

Apart from the tool being a command line tool, another way you can use Rex is to actually reference it in your code (Visual Studio allows you to reference .exe file – not just .dll files). By doing this, you can use Rex to generate random strings that you can use for mocking or for unit test inputs. (AWESOME!)

Here is some sample code (C#) to generate random phone numbers:

using Rex;

RexSettings settings = new RexSettings(@"^\d{3}-\d{3}-\d{4}$");
settings.k = 10;
settings.encoding = CharacterEncoding.ASCII;
settings.seed = (int)DateTime.Now.Ticks;

var results = RexEngine.GenerateMembers(settings);
foreach (var result in results)
{
Console.WriteLine(result);
}

And here is the output for one run:

312-889-8953
999-741-0638
999-896-9342
898-795-0789
496-589-9259
897-910-1576
899-968-9169
846-098-3822
282-169-8069
883-898-2237

More Info:

Rex on MSR: http://research.microsoft.com/en-us/projects/rex/

Rex on the Web (Pex for Fun): http://www.pexforfun.com/Default.aspx?language=CSharp&sample=RegularExpressions

Pex and Moles for Visual Studio: http://research.microsoft.com/en-us/projects/pex/downloads.aspx (white-box testing tool that uses Rex)

Regular Expression Language Reference: http://msdn.microsoft.com/en-us/library/az24scfc.aspx

Sunday, June 28, 2009

RegEx – Preventing matches based on specific characters

from: Finding lines not containing certain words

use the Prevent Match expression:

~(X) Prevents a match when X appears at this point in the expression. For example, real~(ity)matches the "real" in "realty" and "really," but not the "real" in "reality."

Eg: used in the blog post:

{public ~(const|event|delegate|readonly|static readonly|static extern|abstract).*};$ 
finds all public members in C# code.

Wednesday, June 17, 2009

Regex – Phone Numbers

Here is a useful RegEx (.Net) expression to find U.S. phone numbers:

^(?:(?:[\+]?(?<CountryCode>[\d]{1,3}(?:[ ]+|[\-.])))?[(]?(?<AreaCode>[\d]{3})[\-/)]?(?:[ ]+)?)?
(?<Number>(?<Number1>[a-zA-Z0-9]{3})
(?:[\-. ]?)
(?<Number2>[a-zA-Z0-9]{4,}))
(?:[ \-](?:(?:[xX]|ext)[ \-](?<extn>\d{2,5})))?
$

It finds the following formats of numbers:

313-625-6860
(301) 621-6862
1234567890
111 222 3333
(111)-222-3333
1-800-333-4444
327-6116
1-800-REGEXLIB
(610) 310-5555 x 55
(610) 310-5555 ext 55
1 610 310 5555 ext-555
+1 610 310 5555 ext-555

The regex breaks down the phone number into the following groups:

image

Here is how the Regulator’s RegEx analyzer explains it:

^ (anchor to start of string)
Non-capturing Group
  Non-capturing Group
    Any character in "\+"
    ? (zero or one time)
    Capture to <CountryCode>
      Any character in "\d"
      At least 1, but not more than 3 times
      Non-capturing Group
        Any character in " "
        + (one or more times)
                or
        Any character in "\-."
      End Capture
    End Capture
  End Capture
  ? (zero or one time)
  Any character in "("
  ? (zero or one time)
  Capture to <AreaCode>
    Any character in "\d"
    Exactly 3 times
  End Capture
  Any character in "\-/)"
  ? (zero or one time)
  Non-capturing Group
    Any character in " "
    + (one or more times)
  End Capture
  ? (zero or one time)
End Capture
? (zero or one time)


Capture to <Number1>
  Any character in "a-zA-Z0-9"
  Exactly 3 times
End Capture


Non-capturing Group
  Any character in "\-. "
  ? (zero or one time)
End Capture


Capture to <Number2>
  Any character in "a-zA-Z0-9"
  At least 4 times
End Capture


Non-capturing Group
  Any character in " \-"
  Non-capturing Group
    Non-capturing Group
      Any character in "xX"
            or
      ext
    End Capture
    Any character in " \-"
    Capture to <extn>
      Any digit 
      At least 2, but not more than 5 times
    End Capture
  End Capture
End Capture
? (zero or one time)

$ (anchor to end of string)

And here is the code to use it:

private void Test()
        {
            string regex = "^(?:(?:[\\+]?(?<CountryCode>[\\d]{1,3}(?:[ ]+|[\\-.])))?[(]?(?<AreaCode>[\\d]{3})[\\-/" +
")]?(?:[ ]+)?)?\r\n(?<Number>(?<Number1>[a-zA-Z0-9]{3})\r\n(?:[\\-. ]?)\r\n(?<Number2>[a" +
"-zA-Z0-9]{4,}))\r\n(?:[ \\-](?:(?:[xX]|ext)[ \\-](?<extn>\\d{2,5})))?\r\n$";
            System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.Multiline) 
                        | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);
        }