CookBook

/*

* This is a conversion of some of the regular expressions listed in

* the Perl CookBook.

* compile this sample with the command line:

* csc /debug /r:System.Text.RegularExpressions.dll cookbook.cs

* This has been tested with Beta1 of the Framework SDK.

*/

 

using System.Text.RegularExpressions;

using System;

 

class Test

  {

  static void Main()

    {

    // Roman Numbers

    string p1 = "^m*(d?c{0,3}|c[dm])"

      + "(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$";

    string t1 = "vii";

    Match m1 = Regex.Match(t1, p1);

    Console.WriteLine("Match=[" + m1 + "]");

 

    // Swap first two words

    string t2 = "the quick brown fox";

    string p2 = @"(\S+)(\s+)(\S+)";

    Regex x2 = new Regex(p2);

    string r2 = x2.Replace(t2, "$3$2$1", 1);

    Console.WriteLine("Result=[" + r2 + "]");

 

    // Keyword = Value

    string t3 = "myval = 3";

    string p3 = @"(\w+)\s*=\s*(.*)\s*$";

    Match m3 = Regex.Match(t3, p3);

    Console.WriteLine("Group1=[" + m1.Group(1) + "]");

    Console.WriteLine("Group2=[" + m1.Group(2) + "]");

 

    // Line of at least 80 chars

    string t4 = "********************"

      + "******************************"

      + "******************************";

    string p4 = ".{80,}";

    Match m4 = Regex.Match(t4, p4);

    Console.WriteLine("if line >= 80 is = " + m1.Success + "]");

 

    // MM/DD/YY HH:MM:SS

    string t5 = "01/01/01 16:10:01";

    string p5 = @"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)";

    Match m5 = Regex.Match(t5, p5);

    Console.WriteLine("M5=" + m5);

    for (int i = 1; i <= 6; i++)

      Console.WriteLine("Group" + i + "=[" + m5.Group(i) + "]");

 

    // Changing directories (for Windows)

    string t6 = @"C:\Documents and Settings\user1\Desktop\";

    string r6 = Regex.Replace(t6, @"\\user1\\", @"\\user2\\"); // ";

 

    // expanding (%nn) hex escapes

    string t7 = "%41";

    string p7 = "%([0-9A-Fa-f][0-9A-Fa-f])";

    string r7 = Regex.Replace(t7, p7, HexConvert);

    Console.WriteLine("R7=" + r7);

 

    // deleting C comments (imperfectly)

    string t8 = @"

/*

* this is an old cstyle comment block

*/

foo();

";

    string p8 = @"

  /\*  # match the opening delimiter

  .*?    # match a minimal numer of chracters

  \*/    # match the closing delimiter

";

    string r8 = Regex.Replace(t8, p8, "", "xs");

    Console.WriteLine("r8="+r8);

 

    // Removing leading and trailing whitespace

    string t9a = "     leading";

    string p9a = @"^\s+";

    string r9a = Regex.Replace(t9a, p9a, "");

    Console.WriteLine("r9b=" + r9a);

 

    string t9b = "trailing    ";

    string p9b = @"\s+$";

    string r9b = Regex.Replace(t9b, p9b, "");

    Console.WriteLine("r9b=" + r9b);

 

    // turning \ followed by n into a real newline

    string t10 = @"\ntest\n";

    string r10 = Regex.Replace(t10, @"\\n", "\n");

    Console.WriteLine("r10=" + r10);

 

    // IP address

    string t11 = "55.54.53.52";

    string p11 = "^" +

      @"([01]?\d\d|2[0-4]\d|25[0-5])\." +

      @"([01]?\d\d|2[0-4]\d|25[0-5])\." +

      @"([01]?\d\d|2[0-4]\d|25[0-5])\." +

      @"([01]?\d\d|2[0-4]\d|25[0-5])" +

      "$";

    Match m11 = Regex.Match(t11, p11);

    Console.WriteLine("M11=" + m11);

    Console.WriteLine("Group1=" + m11.Group(1));

    Console.WriteLine("Group2=" + m11.Group(2));

    Console.WriteLine("Group3=" + m11.Group(3));

    Console.WriteLine("Group4=" + m11.Group(4));

 

    // removing leading path from filename

 

    string t12 = @"c:\file.txt";

    string p12 = @"^.*\\";

    string r12 = Regex.Replace(t12, p12, "");

    Console.WriteLine("r12=" + r12);

 

    // joining lines in multiline strings

    string t13 = @"this is

a split line";

    string p13 = @"\s*\r?\n\s*";

    string r13 = Regex.Replace(t13, p13, " ");

    Console.WriteLine("r13=" + r13);

 

    // extracting all numbers from a string

    string t14 = @"

test 1

test 2.3

test 47

";

    string p14 = @"(\d+\.?\d*|\.\d+)";

    MatchCollection mc14 = Regex.Matches(t14, p14);

    foreach (Match m in mc14)

      Console.WriteLine("Match=" + m);

 

    // finding all caps words

    string t15 = "This IS a Test OF ALL Caps";

    string p15 = @"(\b[^\Wa-z0-9_]+\b)";

    MatchCollection mc15 = Regex.Matches(t15, p15);

    foreach (Match m in mc15)

      Console.WriteLine("Match=" + m);

 

    // find all lowercase words

    string t16 = "This is A Test of lowercase";

    string p16 = @"(\b[^\WA-Z0-9_]+\b)";

    MatchCollection mc16 = Regex.Matches(t16, p16);

    foreach (Match m in mc16)

      Console.WriteLine("Match=" + m);

 

    // find all initial caps

    string t17 = "This is A Test of Initial Caps";

    string p17 = @"(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)";

    MatchCollection mc17 = Regex.Matches(t17, p17);

    foreach (Match m in mc17)

      Console.WriteLine("Match=" + m);

 

    // find links in simple html

    string t18 = @"

first tag text

next tag text

";

    string p18 = @"]*?HREF\s*=\s*[""']?([^'"" >]+?)[ '""]?>";

    MatchCollection mc18 = Regex.Matches(t18, p18, "si");

    foreach (Match m in mc18)

      {

      Console.WriteLine("Match=" + m);

      Console.WriteLine("Group1=" + m.Group(1));

      }

 

    // finding middle initial

    string t19 = "Hanley A. Strappman";

    string p19 = @"^\S+\s+(\S)\S*\s+\S";

    Match m19 = Regex.Match(t19, p19);

    Console.WriteLine("Initial=" + m19.Group(1));

 

    // changing inch marks to quotes

    string t20 = @"2' 2"" ";

    string p20 = "\"([^\"]*)";

    string r20 = Regex.Replace(t20, p20, "``$1''");

    Console.WriteLine("Result=" + r20);

 

    }

 

  static int[] hexval = new int[128];

  static Test()

    {

    InitHex();

    }

  static void InitHex()

    {

    hexval['0'] = 0;

    hexval['1'] = 1;

    hexval['2'] = 2;

    hexval['3'] = 3;

    hexval['4'] = 4;

    hexval['5'] = 5;

    hexval['6'] = 6;

    hexval['7'] = 7;

    hexval['8'] = 8;

    hexval['9'] = 9;

    hexval['A'] = 10;

    hexval['B'] = 11;

    hexval['C'] = 12;

    hexval['D'] = 13;

    hexval['E'] = 14;

    hexval['F'] = 15;

    hexval['a'] = 10;

    hexval['b'] = 11;

    hexval['c'] = 12;

    hexval['d'] = 13;

    hexval['e'] = 14;

    hexval['f'] = 15;

    }

  static MatchEvaluator HexConvert = new MatchEvaluator(Hex);

  static string Hex(Match m)

    {

    string s = m.Group(1).ToString();

    int i0 = hexval[s[0]];

    int i1 = hexval[s[1]];

    string r = new string((char)((i0 << 4) | i1),1);

    return r;

    }

 

 

  }

 

 

 

 

 

 

posted @ 2004-02-23 15:15  dudu  阅读(1741)  评论(0编辑  收藏  举报