Logo ROOT  
Reference Guide
TPRegexp.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Eddy Offermann 24/06/05
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TPRegexp
13 #define ROOT_TPRegexp
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TPRegexp //
18 // //
19 // C++ Wrapper for the "Perl Compatible Regular Expressions" library //
20 // The PCRE lib can be found at: //
21 // http://www.pcre.org/ //
22 // //
23 // Extensive documentation about Regular expressions in Perl can be //
24 // found at : //
25 // http://perldoc.perl.org/perlre.html //
26 // //
27 //////////////////////////////////////////////////////////////////////////
28 
29 #include "Rtypes.h"
30 #include "TString.h"
31 #include "TArrayI.h"
32 
33 struct PCREPriv_t;
34 
35 
36 class TPRegexp {
37 
38 protected:
39  enum {
40  kPCRE_GLOBAL = 0x80000000,
41  kPCRE_OPTIMIZE = 0x40000000,
42  kPCRE_DEBUG_MSGS = 0x20000000,
43  kPCRE_INTMASK = 0x0FFF
44  };
45 
47  PCREPriv_t *fPriv;
49 
51 
52  void Compile();
53  void Optimize();
54  UInt_t ParseMods(const TString &mods) const;
55  Int_t ReplaceSubs(const TString &s, TString &final,
56  const TString &replacePattern,
57  Int_t *ovec, Int_t nmatch) const;
58 
59  Int_t MatchInternal(const TString& s, Int_t start,
60  Int_t nMaxMatch, TArrayI *pos=0) const;
61 
62  Int_t SubstituteInternal(TString &s, const TString &replace,
63  Int_t start, Int_t nMaxMatch0,
64  Bool_t doDollarSubst) const;
65 
66 public:
67  TPRegexp();
68  TPRegexp(const TString &pat);
69  TPRegexp(const TPRegexp &p);
70  virtual ~TPRegexp();
71 
72  Bool_t IsValid() const;
73 
74  Int_t Match(const TString &s, const TString &mods="",
75  Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos=0);
76  TObjArray *MatchS(const TString &s, const TString &mods="",
77  Int_t start=0, Int_t nMaxMatch=10);
78  Bool_t MatchB(const TString &s, const TString &mods="",
79  Int_t start=0, Int_t nMaxMatch=10) {
80  return (Match(s,mods,start,nMaxMatch) > 0); }
81  Int_t Substitute(TString &s, const TString &replace,
82  const TString &mods="", Int_t start=0,
83  Int_t nMatchMax=10);
84 
85  TString GetPattern() const { return fPattern; }
86  TString GetModifiers() const;
87 
88  TPRegexp &operator=(const TPRegexp &p);
89 
91  static void SetThrowAtCompileError(Bool_t throwp);
92 
93  ClassDef(TPRegexp,0) // Perl Compatible Regular Expression Class
94 };
95 
96 
97 class TPMERegexp : protected TPRegexp {
98 
99 private:
100  TPMERegexp& operator=(const TPMERegexp&); // Not implemented
101 
102 protected:
103  Int_t fNMaxMatches; // maximum number of matches
104  Int_t fNMatches; // number of matches returned from last pcre_exec call
105  TArrayI fMarkers; // last set of indexes of matches
106 
107  TString fLastStringMatched; // copy of the last TString matched
108  void *fAddressOfLastString; // used for checking for change of TString in global match
109 
110  Int_t fLastGlobalPosition; // end of last match when kPCRE_GLOBAL is set
111 
112 public:
113  TPMERegexp();
114  TPMERegexp(const TString& s, const TString& opts = "", Int_t nMatchMax = 10);
115  TPMERegexp(const TString& s, UInt_t opts, Int_t nMatchMax = 10);
116  TPMERegexp(const TPMERegexp& r);
117 
118  virtual ~TPMERegexp() {}
119 
120  void Reset(const TString& s, const TString& opts = "", Int_t nMatchMax = -1);
121  void Reset(const TString& s, UInt_t opts, Int_t nMatchMax = -1);
122 
123  Int_t GetNMaxMatches() const { return fNMaxMatches; }
125 
127  void AssignGlobalState(const TPMERegexp& re);
128  void ResetGlobalState();
129 
130  Int_t Match(const TString& s, UInt_t start = 0);
131  Int_t Split(const TString& s, Int_t maxfields = 0);
132  Int_t Substitute(TString& s, const TString& r, Bool_t doDollarSubst=kTRUE);
133 
134  Int_t NMatches() const { return fNMatches; }
136 
137  virtual void Print(Option_t* option="");
138 
139  ClassDef(TPMERegexp, 0); // Wrapper for Perl-like regular expression matching.
140 };
141 
142 
143 class TStringToken : public TString {
144 
145 protected:
150 
151 public:
152  TStringToken(const TString& fullStr, const TString& splitRe, Bool_t retVoid=kFALSE);
153  virtual ~TStringToken() {}
154 
155  Bool_t NextToken();
156  Bool_t AtEnd() const { return fPos >= fFullStr.Length(); }
157 
158  ClassDef(TStringToken,0) // String tokenizer using PCRE for finding next tokens.
159 };
160 
161 #endif
TPMERegexp::SetNMaxMatches
void SetNMaxMatches(Int_t nm)
Definition: TPRegexp.h:124
TPMERegexp::fAddressOfLastString
void * fAddressOfLastString
Definition: TPRegexp.h:108
TPMERegexp
Definition: TPRegexp.h:97
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TPMERegexp::TPMERegexp
TPMERegexp()
Default constructor. This regexp will match an empty string.
Definition: TPRegexp.cxx:588
TObjArray
Definition: TObjArray.h:37
TPMERegexp::~TPMERegexp
virtual ~TPMERegexp()
Definition: TPRegexp.h:118
TPMERegexp::Split
Int_t Split(const TString &s, Int_t maxfields=0)
Splits into at most maxfields.
Definition: TPRegexp.cxx:765
TPRegexp::Match
Int_t Match(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos=0)
The number of matches is returned, this equals the full match + sub-pattern matches.
Definition: TPRegexp.cxx:339
TPRegexp::Substitute
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...
Definition: TPRegexp.cxx:472
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TStringToken
Definition: TPRegexp.h:143
TStringToken::fFullStr
const TString fFullStr
Definition: TPRegexp.h:146
TPMERegexp::Print
virtual void Print(Option_t *option="")
Print the regular expression and modifier options.
Definition: TPRegexp.cxx:933
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
Int_t
int Int_t
Definition: RtypesCore.h:45
TPMERegexp::Match
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:708
TStringToken::AtEnd
Bool_t AtEnd() const
Definition: TPRegexp.h:156
TPRegexp::fgThrowAtCompileError
static Bool_t fgThrowAtCompileError
Definition: TPRegexp.h:50
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
TPRegexp::kPCRE_INTMASK
@ kPCRE_INTMASK
Definition: TPRegexp.h:43
TPRegexp::operator=
TPRegexp & operator=(const TPRegexp &p)
Assignment operator.
Definition: TPRegexp.cxx:92
TString
Definition: TString.h:136
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TString.h
TPRegexp::MatchS
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.
Definition: TPRegexp.cxx:370
TPMERegexp::Substitute
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:874
TPRegexp::GetModifiers
TString GetModifiers() const
Return PCRE modifier options as string.
Definition: TPRegexp.cxx:180
bool
TPMERegexp::fLastGlobalPosition
Int_t fLastGlobalPosition
Definition: TPRegexp.h:110
TPMERegexp::operator[]
TString operator[](Int_t)
Returns the sub-string from the internal fMarkers vector.
Definition: TPRegexp.cxx:918
TStringToken::fPos
Int_t fPos
Definition: TPRegexp.h:149
TPMERegexp::GetNMaxMatches
Int_t GetNMaxMatches() const
Definition: TPRegexp.h:123
TPMERegexp::fNMatches
Int_t fNMatches
Definition: TPRegexp.h:104
TPRegexp::kPCRE_OPTIMIZE
@ kPCRE_OPTIMIZE
Definition: TPRegexp.h:41
TPRegexp::MatchB
Bool_t MatchB(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Definition: TPRegexp.h:78
TPRegexp::TPRegexp
TPRegexp()
Default ctor.
Definition: TPRegexp.cxx:51
TPRegexp::Compile
void Compile()
Compile the fPattern.
Definition: TPRegexp.cxx:198
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TPMERegexp::fMarkers
TArrayI fMarkers
Definition: TPRegexp.h:105
TPRegexp::IsValid
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation.
Definition: TPRegexp.cxx:490
TPMERegexp::Reset
void Reset(const TString &s, const TString &opts="", Int_t nMatchMax=-1)
Reset the pattern and options.
Definition: TPRegexp.cxx:653
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TPRegexp::ReplaceSubs
Int_t ReplaceSubs(const TString &s, TString &final, const TString &replacePattern, Int_t *ovec, Int_t nmatch) const
Returns the number of expanded '$' constructs.
Definition: TPRegexp.cxx:251
TPMERegexp::ResetGlobalState
void ResetGlobalState()
Reset state of global match.
Definition: TPRegexp.cxx:696
TPMERegexp::NMatches
Int_t NMatches() const
Definition: TPRegexp.h:134
TPRegexp::kPCRE_DEBUG_MSGS
@ kPCRE_DEBUG_MSGS
Definition: TPRegexp.h:42
TArrayI.h
TPMERegexp::GetGlobalPosition
Int_t GetGlobalPosition() const
Definition: TPRegexp.h:126
TPRegexp::kPCRE_GLOBAL
@ kPCRE_GLOBAL
Definition: TPRegexp.h:40
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TPRegexp::SetThrowAtCompileError
static void SetThrowAtCompileError(Bool_t throwp)
Set static flag controlling whether exception should be thrown upon an error during regular expressio...
Definition: TPRegexp.cxx:508
TPMERegexp::fLastStringMatched
TString fLastStringMatched
Definition: TPRegexp.h:107
unsigned int
TStringToken::~TStringToken
virtual ~TStringToken()
Definition: TPRegexp.h:153
TPRegexp::fPriv
PCREPriv_t * fPriv
Definition: TPRegexp.h:47
TPRegexp::fPattern
TString fPattern
Definition: TPRegexp.h:46
TPMERegexp::AssignGlobalState
void AssignGlobalState(const TPMERegexp &re)
Copy global-match state from 're; so that this regexp can continue parsing the string from where 're'...
Definition: TPRegexp.cxx:684
TPRegexp
Definition: TPRegexp.h:36
TStringToken::NextToken
Bool_t NextToken()
Get the next token, it is stored in this TString.
Definition: TPRegexp.cxx:977
TPMERegexp::operator=
TPMERegexp & operator=(const TPMERegexp &)
TPRegexp::SubstituteInternal
Int_t SubstituteInternal(TString &s, const TString &replace, Int_t start, Int_t nMaxMatch0, Bool_t doDollarSubst) const
Perform pattern substitution with optional back-ref replacement.
Definition: TPRegexp.cxx:396
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TPRegexp::Optimize
void Optimize()
Send the pattern through the optimizer.
Definition: TPRegexp.cxx:230
TPRegexp::MatchInternal
Int_t MatchInternal(const TString &s, Int_t start, Int_t nMaxMatch, TArrayI *pos=0) const
Perform the actual matching - protected method.
Definition: TPRegexp.cxx:307
TPRegexp::ParseMods
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
Definition: TPRegexp.cxx:135
TPRegexp::GetThrowAtCompileError
static Bool_t GetThrowAtCompileError()
Get value of static flag controlling whether exception should be thrown upon an error during regular ...
Definition: TPRegexp.cxx:499
TStringToken::fReturnVoid
Bool_t fReturnVoid
Definition: TPRegexp.h:148
TPRegexp::GetPattern
TString GetPattern() const
Definition: TPRegexp.h:85
TPMERegexp::fNMaxMatches
Int_t fNMaxMatches
Definition: TPRegexp.h:103
TGeant4Unit::nm
static constexpr double nm
Definition: TGeant4SystemOfUnits.h:111
TStringToken::TStringToken
TStringToken(const TString &fullStr, const TString &splitRe, Bool_t retVoid=kFALSE)
Constructor.
Definition: TPRegexp.cxx:965
TStringToken::fSplitRe
TPRegexp fSplitRe
Definition: TPRegexp.h:147
Rtypes.h
TArrayI
Definition: TArrayI.h:27
int
TPRegexp::~TPRegexp
virtual ~TPRegexp()
Cleanup.
Definition: TPRegexp.cxx:80
TPRegexp::fPCREOpts
UInt_t fPCREOpts
Definition: TPRegexp.h:48