Logo ROOT   6.18/05
Reference Guide
regexp_pme.C File Reference

Detailed Description

Class TPMERegexp - API similar to PME - PCRE Made Easy Tries to be as close as possible to PERL syntax and functionality.

Extension of TPRegexp class, see also macro 'regexp.C'.

Global matching
----------------------------------------------------------------
Regexp='ba[rz]', Opts='g'
last string='foobarbaz'
number of matches = 1
0 - bar
Regexp='ba[rz]', Opts='g'
last string='foobarbaz'
number of matches = 1
0 - baz
Global matching with back-refs
----------------------------------------------------------------
Regexp='(ba[rz])', Opts='g'
last string='foobarbaz'
number of matches = 2
0 - bar
1 - bar
Regexp='(ba[rz])', Opts='g'
last string='foobarbaz'
number of matches = 2
0 - baz
1 - baz
Matching with nested back-refs
----------------------------------------------------------------
Regexp='([\w\.-]+)@((\d+)\.(\d+)\.(\d+)\.(\d+))', Opts=''
last string='matevz.tadel@137.138.170.210'
number of matches = 7
0 - matevz.tadel@137.138.170.210
1 - matevz.tadel
2 - 137.138.170.210
3 - 137
4 - 138
5 - 170
6 - 210
Split
----------------------------------------------------------------
Regexp=':', Opts=''
last string='root:x:0:0:root:/root:/bin/bash'
number of matches = 7
0 - root
1 - x
2 - 0
3 - 0
4 - root
5 - /root
6 - /bin/bash
Split with maxfields=5
----------------------------------------------------------------
Regexp=':', Opts=''
last string='root:x:0:0:root:/root:/bin/bash'
number of matches = 5
0 - root
1 - x
2 - 0
3 - 0
4 - root:/root:/bin/bash
Split with empty elements in the middle and at the end
maxfields=0, so trailing empty elements are dropped
----------------------------------------------------------------
Regexp=':', Opts=''
last string='root::0:0:root:/root::'
number of matches = 6
0 - root
1 -
2 - 0
3 - 0
4 - root
5 - /root
Split with empty elements at the beginning and end
maxfields=-1, so trailing empty elements are kept
----------------------------------------------------------------
Regexp=':', Opts=''
last string=':x:0:0:root::'
number of matches = 7
0 -
1 - x
2 - 0
3 - 0
4 - root
5 -
6 -
Split with no pattern in string
----------------------------------------------------------------
Regexp=':', Opts=''
last string='A dummy line of text.'
number of matches = 1
0 - A dummy line of text.
Split with regexp potentially matching a null string
----------------------------------------------------------------
Regexp=' *', Opts=''
last string='hi there'
number of matches = 7
0 - h
1 - i
2 - t
3 - h
4 - e
5 - r
6 - e
Split on patteren with back-refs
----------------------------------------------------------------
Regexp='([,-])', Opts=''
last string='1-10,20'
number of matches = 5
0 - 1
1 - -
2 - 10
3 - ,
4 - 20
Substitute
----------------------------------------------------------------
Regexp='(\d+)\.(\d+)\.(\d+)\.(\d+)', Opts=''
Substitute '137.138.170.210','$4.$3.$2.$1' => '210.170.138.137'
Global substitute
----------------------------------------------------------------
Regexp='(\w+)\.(\w+)@[\w\.-]+', Opts='g'
Substitute 'rene.brun@cern.ch, philippe.canal@fnal.gov, fons.rademakers@cern.ch','\u$1 \U$2\E' => 'Rene BRUN, Philippe CANAL, Fons RADEMAKERS'
void regexp_pme()
{
static const char *underline =
"----------------------------------------------------------------\n";
// Match tests
{
printf("Global matching\n%s", underline);
TPMERegexp re("ba[rz]", "g");
TString m("foobarbaz");
while (re.Match(m))
re.Print("all");
printf("\n");
printf("Global matching with back-refs\n%s", underline);
TPMERegexp re1("(ba[rz])", "g");
TString m1("foobarbaz");
while (re1.Match(m1))
re1.Print("all");
printf("\n");
printf("Matching with nested back-refs\n%s", underline);
TPMERegexp re2("([\\w\\.-]+)@((\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+))");
TString m2("matevz.tadel@137.138.170.210");
re2.Match(m2);
re2.Print("all");
printf("\n");
}
// Split tests
{
printf("Split\n%s", underline);
TPMERegexp re(":");
TString m("root:x:0:0:root:/root:/bin/bash");
re.Split(m);
re.Print("all");
printf("\n");
printf("Split with maxfields=5\n%s", underline);
re.Split(m, 5);
re.Print("all");
printf("\n");
printf("Split with empty elements in the middle and at the end\n"
"maxfields=0, so trailing empty elements are dropped\n%s", underline);
m = "root::0:0:root:/root::";
re.Split(m);
re.Print("all");
printf("\n");
printf("Split with empty elements at the beginning and end\n"
"maxfields=-1, so trailing empty elements are kept\n%s", underline);
m = ":x:0:0:root::";
re.Split(m, -1);
re.Print("all");
printf("\n");
printf("Split with no pattern in string\n%s", underline);
m = "A dummy line of text.";
re.Split(m);
re.Print("all");
printf("\n");
}
{
printf("Split with regexp potentially matching a null string \n%s", underline);
TPMERegexp re(" *");
TString m("hi there");
re.Split(m);
re.Print("all");
printf("\n");
}
{
printf("Split on patteren with back-refs\n%s", underline);
TPMERegexp re("([,-])");
TString m("1-10,20");
re.Split(m);
re.Print("all");
printf("\n");
}
// Substitute tests
{
printf("Substitute\n%s", underline);
TPMERegexp re("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
TString m("137.138.170.210");
TString r("$4.$3.$2.$1");
TString s(m); re.Substitute(s, r);
re.Print();
printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
printf("\n");
}
{
printf("Global substitute\n%s", underline);
TPMERegexp re("(\\w+)\\.(\\w+)@[\\w\\.-]+", "g");
TString m("rene.brun@cern.ch, philippe.canal@fnal.gov, fons.rademakers@cern.ch");
TString r("\\u$1 \\U$2\\E");
TString s(m); re.Substitute(s, r);
re.Print();
printf("Substitute '%s','%s' => '%s'\n", m.Data(), r.Data(), s.Data());
printf("\n");
}
}
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
Author
Eddy Offermann

Definition in file regexp_pme.C.