ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
regexp_pme.C
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------------
2 //
3 // class TPMERegexp - API similar to PME - PCRE Made Easy
4 // Tries to be as close as possible to PERL syntax and functionality.
5 //
6 // Extension of TPRegexp class, see also macro 'regexp.C'.
7 //
8 //-------------------------------------------------------------------------------------------
9 
10 void regexp_pme()
11 {
12  static const char *underline =
13  "----------------------------------------------------------------\n";
14 
15 
16  // Match tests
17 
18  {
19  printf("Global matching\n%s", underline);
20  TPMERegexp re("ba[rz]", "g");
21  TString m("foobarbaz");
22  while (re.Match(m))
23  re.Print("all");
24  printf("\n");
25 
26  printf("Global matching with back-refs\n%s", underline);
27  TPMERegexp re1("(ba[rz])", "g");
28  TString m1("foobarbaz");
29  while (re1.Match(m1))
30  re1.Print("all");
31  printf("\n");
32 
33  printf("Matching with nested back-refs\n%s", underline);
34  TPMERegexp re2("([\\w\\.-]+)@((\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+))");
35  TString m2("matevz.tadel@137.138.170.210");
36  re2.Match(m2);
37  re2.Print("all");
38  printf("\n");
39  }
40 
41 
42  // Split tests
43 
44  {
45  printf("Split\n%s", underline);
46  TPMERegexp re(":");
47  TString m("root:x:0:0:root:/root:/bin/bash");
48  re.Split(m);
49  re.Print("all");
50  printf("\n");
51 
52  printf("Split with maxfields=5\n%s", underline);
53  re.Split(m, 5);
54  re.Print("all");
55  printf("\n");
56 
57  printf("Split with empty elements in the middle and at the end\n"
58  "maxfields=0, so trailing empty elements are dropped\n%s", underline);
59  m = "root::0:0:root:/root::";
60  re.Split(m);
61  re.Print("all");
62  printf("\n");
63 
64  printf("Split with empty elements at the beginning and end\n"
65  "maxfields=-1, so trailing empty elements are kept\n%s", underline);
66  m = ":x:0:0:root::";
67  re.Split(m, -1);
68  re.Print("all");
69  printf("\n");
70 
71  printf("Split with no pattern in string\n%s", underline);
72  m = "A dummy line of text.";
73  re.Split(m);
74  re.Print("all");
75  printf("\n");
76  }
77 
78  {
79  printf("Split with regexp potentially matching a null string \n%s", underline);
80  TPMERegexp re(" *");
81  TString m("hi there");
82  re.Split(m);
83  re.Print("all");
84  printf("\n");
85  }
86 
87  {
88  printf("Split on patteren with back-refs\n%s", underline);
89  TPMERegexp re("([,-])");
90  TString m("1-10,20");
91  re.Split(m);
92  re.Print("all");
93  printf("\n");
94  }
95 
96 
97  // Substitute tests
98 
99  {
100  printf("Substitute\n%s", underline);
101  TPMERegexp re("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
102  TString m("137.138.170.210");
103  TString r("$4.$3.$2.$1");
104  TString s(m); re.Substitute(s, r);
105  re.Print();
106  printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
107  printf("\n");
108  }
109 
110  {
111  printf("Global substitute\n%s", underline);
112  TPMERegexp re("(\\w+)\\.(\\w+)@[\\w\\.-]+", "g");
113  TString m("rene.brun@cern.ch, philippe.canal@fnal.gov, fons.rademakers@cern.ch");
114  TString r("\\u$1 \\U$2\\E");
115  TString s(m); re.Substitute(s, r);
116  re.Print();
117  printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
118  printf("\n");
119  }
120 }
Basic string class.
Definition: TString.h:137
const char * Data() const
Definition: TString.h:349
void regexp_pme()
Definition: regexp_pme.C:10
ROOT::R::TRInterface & r
Definition: Object.C:4
Int_t Split(const TString &s, Int_t maxfields=0)
Splits into at most maxfields.
Definition: TPRegexp.cxx:761
TMarker * m
Definition: textangle.C:8
virtual void Print(Option_t *option="")
Print the regular expression and modifier options.
Definition: TPRegexp.cxx:929
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Int_t Match(const TString &s, UInt_t start=0)
Runs a match on s against the regex 'this' was created with.
Definition: TPRegexp.cxx:704
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Definition: TPRegexp.h:103
Int_t Substitute(TString &s, const TString &r, Bool_t doDollarSubst=kTRUE)
Substitute matching part of s with r, dollar back-ref substitution is performed if doDollarSubst is t...
Definition: TPRegexp.cxx:870