Logo ROOT   6.19/01
Reference Guide
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Eddy Offermann 24/06/05
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  *************************************************************************/
12 #ifndef ROOT_TPRegexp
13 #define ROOT_TPRegexp
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 //////////////////////////////////////////////////////////////////////////
29 #include "Rtypes.h"
30 #include "TString.h"
31 #include "TArrayI.h"
33 struct PCREPriv_t;
36 class TPRegexp {
38 protected:
39  enum {
40  kPCRE_GLOBAL = 0x80000000,
41  kPCRE_OPTIMIZE = 0x40000000,
42  kPCRE_DEBUG_MSGS = 0x20000000,
44  };
47  PCREPriv_t *fPriv;
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;
59  Int_t MatchInternal(const TString& s, Int_t start,
60  Int_t nMaxMatch, TArrayI *pos=0) const;
62  Int_t SubstituteInternal(TString &s, const TString &replace,
63  Int_t start, Int_t nMaxMatch0,
64  Bool_t doDollarSubst) const;
66 public:
67  TPRegexp();
68  TPRegexp(const TString &pat);
69  TPRegexp(const TPRegexp &p);
70  virtual ~TPRegexp();
72  Bool_t IsValid() const;
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);
85  TString GetPattern() const { return fPattern; }
86  TString GetModifiers() const;
88  TPRegexp &operator=(const TPRegexp &p);
91  static void SetThrowAtCompileError(Bool_t throwp);
93  ClassDef(TPRegexp,0) // Perl Compatible Regular Expression Class
94 };
97 class TPMERegexp : protected TPRegexp {
99 private:
100  TPMERegexp& operator=(const TPMERegexp&); // Not implemented
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
107  TString fLastStringMatched; // copy of the last TString matched
108  void *fAddressOfLastString; // used for checking for change of TString in global match
110  Int_t fLastGlobalPosition; // end of last match when kPCRE_GLOBAL is set
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);
118  virtual ~TPMERegexp() {}
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);
123  Int_t GetNMaxMatches() const { return fNMaxMatches; }
127  void AssignGlobalState(const TPMERegexp& re);
128  void ResetGlobalState();
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);
134  Int_t NMatches() const { return fNMatches; }
137  virtual void Print(Option_t* option="");
139  ClassDef(TPMERegexp, 0); // Wrapper for Perl-like regular expression matching.
140 };
143 class TStringToken : public TString {
145 protected:
151 public:
152  TStringToken(const TString& fullStr, const TString& splitRe, Bool_t retVoid=kFALSE);
153  virtual ~TStringToken() {}
155  Bool_t NextToken();
156  Bool_t AtEnd() const { return fPos >= fFullStr.Length(); }
158  ClassDef(TStringToken,0) // String tokenizer using PCRE for finding next tokens.
159 };
161 #endif
Int_t NMatches() const
Definition: TPRegexp.h:134
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
TString fLastStringMatched
Definition: TPRegexp.h:107
Int_t GetGlobalPosition() const
Definition: TPRegexp.h:126
An array of TObjects.
Definition: TObjArray.h:37
Int_t fPos
Definition: TPRegexp.h:149
Int_t fNMatches
Definition: TPRegexp.h:104
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
Bool_t MatchB(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Definition: TPRegexp.h:78
TPMERegexp & operator=(const TPMERegexp &)
const char Option_t
Definition: RtypesCore.h:62
TArrayI fMarkers
Definition: TPRegexp.h:105
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
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
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
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
virtual ~TPRegexp()
Definition: TPRegexp.cxx:80
Basic string class.
Definition: TString.h:131
TString GetModifiers() const
Return PCRE modifier options as string.
Definition: TPRegexp.cxx:180
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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
TPRegexp fSplitRe
Definition: TPRegexp.h:147
Array of integers (32 bits per element).
Definition: TArrayI.h:27
PCREPriv_t * fPriv
Definition: TPRegexp.h:47
Int_t fNMaxMatches
Definition: TPRegexp.h:103
TStringToken(const TString &fullStr, const TString &splitRe, Bool_t retVoid=kFALSE)
Definition: TPRegexp.cxx:965
#define ClassDef(name, id)
Definition: Rtypes.h:326
Provides iteration through tokens of a given string.
Definition: TPRegexp.h:143
void Compile()
Compile the fPattern.
Definition: TPRegexp.cxx:198
static constexpr double s
virtual ~TPMERegexp()
Definition: TPRegexp.h:118
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation...
Definition: TPRegexp.cxx:490
void SetNMaxMatches(Int_t nm)
Definition: TPRegexp.h:124
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:765
void * fAddressOfLastString
Definition: TPRegexp.h:108
TPRegexp & operator=(const TPRegexp &p)
Assignment operator.
Definition: TPRegexp.cxx:92
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Print(Option_t *option="")
Print the regular expression and modifier options.
Definition: TPRegexp.cxx:933
Ssiz_t Length() const
Definition: TString.h:405
Bool_t AtEnd() const
Definition: TPRegexp.h:156
const Bool_t kFALSE
Definition: RtypesCore.h:88
UInt_t fPCREOpts
Definition: TPRegexp.h:48
Default constructor. This regexp will match an empty string.
Definition: TPRegexp.cxx:588
virtual ~TStringToken()
Definition: TPRegexp.h:153
static constexpr double nm
Bool_t NextToken()
Get the next token, it is stored in this TString.
Definition: TPRegexp.cxx:977
static Bool_t fgThrowAtCompileError
Definition: TPRegexp.h:50
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
Definition: TPRegexp.cxx:135
static Bool_t GetThrowAtCompileError()
Get value of static flag controlling whether exception should be thrown upon an error during regular ...
Definition: TPRegexp.cxx:499
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
const TString fFullStr
Definition: TPRegexp.h:146
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Definition: TPRegexp.h:97
Int_t fLastGlobalPosition
Definition: TPRegexp.h:110
Bool_t fReturnVoid
Definition: TPRegexp.h:148
Int_t GetNMaxMatches() const
Definition: TPRegexp.h:123
void Reset(const TString &s, const TString &opts="", Int_t nMatchMax=-1)
Reset the pattern and options.
Definition: TPRegexp.cxx:653
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
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
const Bool_t kTRUE
Definition: RtypesCore.h:87
Default ctor.
Definition: TPRegexp.cxx:51
TString GetPattern() const
Definition: TPRegexp.h:85
TString operator[](Int_t)
Returns the sub-string from the internal fMarkers vector.
Definition: TPRegexp.cxx:918
TString fPattern
Definition: TPRegexp.h:46
void Optimize()
Send the pattern through the optimizer.
Definition: TPRegexp.cxx:230
void ResetGlobalState()
Reset state of global match.
Definition: TPRegexp.cxx:696