Logo ROOT   6.16/01
Reference Guide
regexp_pme.C
Go to the documentation of this file.
1/// \file
2/// \ingroup Tutorials
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/// \macro_output
9/// \macro_code
10///
11/// \author Eddy Offermann
12
13void regexp_pme()
14{
15 static const char *underline =
16 "----------------------------------------------------------------\n";
17
18
19 // Match tests
20
21 {
22 printf("Global matching\n%s", underline);
23 TPMERegexp re("ba[rz]", "g");
24 TString m("foobarbaz");
25 while (re.Match(m))
26 re.Print("all");
27 printf("\n");
28
29 printf("Global matching with back-refs\n%s", underline);
30 TPMERegexp re1("(ba[rz])", "g");
31 TString m1("foobarbaz");
32 while (re1.Match(m1))
33 re1.Print("all");
34 printf("\n");
35
36 printf("Matching with nested back-refs\n%s", underline);
37 TPMERegexp re2("([\\w\\.-]+)@((\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+))");
38 TString m2("matevz.tadel@137.138.170.210");
39 re2.Match(m2);
40 re2.Print("all");
41 printf("\n");
42 }
43
44
45 // Split tests
46
47 {
48 printf("Split\n%s", underline);
49 TPMERegexp re(":");
50 TString m("root:x:0:0:root:/root:/bin/bash");
51 re.Split(m);
52 re.Print("all");
53 printf("\n");
54
55 printf("Split with maxfields=5\n%s", underline);
56 re.Split(m, 5);
57 re.Print("all");
58 printf("\n");
59
60 printf("Split with empty elements in the middle and at the end\n"
61 "maxfields=0, so trailing empty elements are dropped\n%s", underline);
62 m = "root::0:0:root:/root::";
63 re.Split(m);
64 re.Print("all");
65 printf("\n");
66
67 printf("Split with empty elements at the beginning and end\n"
68 "maxfields=-1, so trailing empty elements are kept\n%s", underline);
69 m = ":x:0:0:root::";
70 re.Split(m, -1);
71 re.Print("all");
72 printf("\n");
73
74 printf("Split with no pattern in string\n%s", underline);
75 m = "A dummy line of text.";
76 re.Split(m);
77 re.Print("all");
78 printf("\n");
79 }
80
81 {
82 printf("Split with regexp potentially matching a null string \n%s", underline);
83 TPMERegexp re(" *");
84 TString m("hi there");
85 re.Split(m);
86 re.Print("all");
87 printf("\n");
88 }
89
90 {
91 printf("Split on patteren with back-refs\n%s", underline);
92 TPMERegexp re("([,-])");
93 TString m("1-10,20");
94 re.Split(m);
95 re.Print("all");
96 printf("\n");
97 }
98
99
100 // Substitute tests
101
102 {
103 printf("Substitute\n%s", underline);
104 TPMERegexp re("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
105 TString m("137.138.170.210");
106 TString r("$4.$3.$2.$1");
107 TString s(m); re.Substitute(s, r);
108 re.Print();
109 printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
110 printf("\n");
111 }
112
113 {
114 printf("Global substitute\n%s", underline);
115 TPMERegexp re("(\\w+)\\.(\\w+)@[\\w\\.-]+", "g");
116 TString m("rene.brun@cern.ch, philippe.canal@fnal.gov, fons.rademakers@cern.ch");
117 TString r("\\u$1 \\U$2\\E");
118 TString s(m); re.Substitute(s, r);
119 re.Print();
120 printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
121 printf("\n");
122 }
123}
ROOT::R::TRInterface & r
Definition: Object.C:4
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Definition: TPRegexp.h:97
Basic string class.
Definition: TString.h:131
static constexpr double s
static constexpr double m2
auto * m
Definition: textangle.C:8