A regular expression, often called a pattern, is an expression that describes a set of strings.
They are usually used to give a concise description of a set, without having to list all elements. The Unix utilities like sed and grep make extensive use of regular expressions. Scripting languages like Perl have regular expression engines built directly into their syntax .
Extensive documentation about Regular expressions in Perl can be found at: http://perldoc.perl.org/perlre.html
ROOT has this capability through the use of the P(erl) C(ompatible) R(egular) E(xpression)
Its functionality can be accessed through the TPRegexp and TString class . Note that in patterns taken from Perl all backslash character have to be replaced in the C/C++ strings by two backslashes .
This macro shows several ways how to use the Match/Substitute capabilities of the the TPRegexp class . It can be run as follows :
pepernotenkoek
lekkere walnotenboom
two one three
on 24-09-1959 the world stood still
Check if the email address "fons.rademakers@cern.ch" is valid: TRUE
neutrino proton electron neutrino
void regexp()
{
r1.Substitute(
s1,
"wal$1boom");
cout << s2 << endl;
TString s3(
"on 09/24/1959 the world stood still");
cout << s3 << endl;
TString s4(
"http://fink.sourceforge.net:8080/index/readme.html");
if (nrSubStr > 2) {
cout <<
"protocol: " <<
proto <<
" port: " << port << endl;
}
TString s5(
"fons.rademakers@cern.ch");
TPRegexp r5(
"^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
cout << "Check if the email address \"" << s5 << "\" is valid: " << (r5.MatchB(s5) ? "TRUE" : "FALSE") << endl;
TString s6(
"neutron proton electron neutron");
cout << s6 << endl;
}
Int_t GetLast() const
Return index of last object in array.
TObject * At(Int_t idx) const
Collectable string class.
TObjArray * MatchS(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Returns a TObjArray of matched substrings as TObjString's.
Int_t Substitute(TString &s, const TString &replace, const TString &mods="", Int_t start=0, Int_t nMatchMax=10)
Substitute replaces the string s by a new string in which matching patterns are replaced by the repla...
- Author
- Eddy Offermann
Definition in file regexp.C.