regexp_pme.C

Go to the documentation of this file.
00001 //-------------------------------------------------------------------------------------------
00002 //
00003 // class TPMERegexp - API similar to PME - PCRE Made Easy
00004 // Tries to be as close as possible to PERL syntax and functionality.
00005 //
00006 // Extension of TPRegexp class, see also macro 'regexp.C'.
00007 //
00008 //-------------------------------------------------------------------------------------------
00009 
00010 void regexp_pme()
00011 {
00012    static const char *underline =
00013       "----------------------------------------------------------------\n";
00014 
00015 
00016    // Match tests
00017 
00018    {
00019       printf("Global matching\n%s", underline);
00020       TPMERegexp re("ba[rz]", "g");
00021       TString m("foobarbaz");
00022       while (re.Match(m))
00023          re.Print("all");
00024       printf("\n");
00025 
00026       printf("Global matching with back-refs\n%s", underline);
00027       TPMERegexp re("(ba[rz])", "g");
00028       TString m("foobarbaz");
00029       while (re.Match(m))
00030          re.Print("all");
00031       printf("\n");
00032 
00033       printf("Matching with nested back-refs\n%s", underline);
00034       TPMERegexp re("([\\w\\.-]+)@((\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+))");
00035       TString m("matevz.tadel@137.138.170.210");
00036       re.Match(m);
00037       re.Print("all");
00038       printf("\n");
00039    }
00040 
00041 
00042    // Split tests
00043 
00044    {
00045       printf("Split\n%s", underline);
00046       TPMERegexp re(":");
00047       TString m("root:x:0:0:root:/root:/bin/bash");
00048       re.Split(m);
00049       re.Print("all");
00050       printf("\n");
00051 
00052       printf("Split with maxfields=5\n%s", underline);
00053       re.Split(m, 5);
00054       re.Print("all");
00055       printf("\n");
00056 
00057       printf("Split with empty elements in the middle and at the end\n"
00058              "maxfields=0, so trailing empty elements are dropped\n%s", underline);
00059       m = "root::0:0:root:/root::";
00060       re.Split(m);
00061       re.Print("all");
00062       printf("\n");
00063 
00064       printf("Split with empty elements at the beginning and end\n"
00065              "maxfields=-1, so trailing empty elements are kept\n%s", underline);
00066       m = ":x:0:0:root::";
00067       re.Split(m, -1);
00068       re.Print("all");
00069       printf("\n");
00070 
00071       printf("Split with no pattern in string\n%s", underline);
00072       m = "A dummy line of text.";
00073       re.Split(m);
00074       re.Print("all");
00075       printf("\n");
00076    }
00077 
00078    {
00079       printf("Split with regexp potentially matching a null string \n%s", underline);
00080       TPMERegexp re(" *");
00081       TString m("hi there");
00082       re.Split(m);
00083       re.Print("all");
00084       printf("\n");
00085    }
00086 
00087    {
00088       printf("Split on patteren with back-refs\n%s", underline);
00089       TPMERegexp re("([,-])");
00090       TString m("1-10,20");
00091       re.Split(m);
00092       re.Print("all");
00093       printf("\n");
00094    }
00095 
00096 
00097    // Substitute tests
00098 
00099    {
00100       printf("Substitute\n%s", underline);
00101       TPMERegexp re("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
00102       TString m("137.138.170.210");
00103       TString r("$4.$3.$2.$1");
00104       TString s(m); re.Substitute(s, r);
00105       re.Print();
00106       printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
00107       printf("\n");
00108    }
00109 
00110    {
00111       printf("Global substitute\n%s", underline);
00112       TPMERegexp re("(\\w+)\\.(\\w+)@[\\w\\.-]+", "g");
00113       TString m("rene.brun@cern.ch, philippe.canal@fnal.gov, fons.rademakers@cern.ch");
00114       TString r("\\u$1 \\U$2\\E");
00115       TString s(m); re.Substitute(s, r);
00116       re.Print();
00117       printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
00118       printf("\n");
00119    }
00120 }

Generated on Tue Jul 5 15:44:53 2011 for ROOT_528-00b_version by  doxygen 1.5.1