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
33struct PCREPriv_t;
34
35
36class TPRegexp {
37
38protected:
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
66public:
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
97class TPMERegexp : protected TPRegexp {
98
99private:
100 TPMERegexp& operator=(const TPMERegexp&); // Not implemented
101
102protected:
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
112public:
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
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
143class TStringToken : public TString {
144
145protected:
150
151public:
152 TStringToken(const TString& fullStr, const TString& splitRe, Bool_t retVoid=kFALSE);
153 virtual ~TStringToken() {}
154
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
ROOT::R::TRInterface & r
Definition: Object.C:4
int Int_t
Definition: RtypesCore.h:43
unsigned int UInt_t
Definition: RtypesCore.h:44
const Bool_t kFALSE
Definition: RtypesCore.h:90
bool Bool_t
Definition: RtypesCore.h:61
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassDef(name, id)
Definition: Rtypes.h:322
Array of integers (32 bits per element).
Definition: TArrayI.h:27
An array of TObjects.
Definition: TObjArray.h:37
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Definition: TPRegexp.h:97
Int_t NMatches() const
Definition: TPRegexp.h:134
virtual ~TPMERegexp()
Definition: TPRegexp.h:118
Int_t GetGlobalPosition() const
Definition: TPRegexp.h:126
Int_t fLastGlobalPosition
Definition: TPRegexp.h:110
void ResetGlobalState()
Reset state of global match.
Definition: TPRegexp.cxx:696
Int_t fNMatches
Definition: TPRegexp.h:104
void * fAddressOfLastString
Definition: TPRegexp.h:108
Int_t GetNMaxMatches() const
Definition: TPRegexp.h:123
virtual void Print(Option_t *option="")
Print the regular expression and modifier options.
Definition: TPRegexp.cxx:933
Int_t Split(const TString &s, Int_t maxfields=0)
Splits into at most maxfields.
Definition: TPRegexp.cxx:765
TPMERegexp & operator=(const TPMERegexp &)
TPMERegexp()
Default constructor. This regexp will match an empty string.
Definition: TPRegexp.cxx:588
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
TString operator[](Int_t)
Returns the sub-string from the internal fMarkers vector.
Definition: TPRegexp.cxx:918
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
void SetNMaxMatches(Int_t nm)
Definition: TPRegexp.h:124
void Reset(const TString &s, const TString &opts="", Int_t nMatchMax=-1)
Reset the pattern and options.
Definition: TPRegexp.cxx:653
Int_t fNMaxMatches
Definition: TPRegexp.h:103
TArrayI fMarkers
Definition: TPRegexp.h:105
TString fLastStringMatched
Definition: TPRegexp.h:107
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()
Default ctor.
Definition: TPRegexp.cxx:51
void Compile()
Compile the fPattern.
Definition: TPRegexp.cxx:198
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
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation.
Definition: TPRegexp.cxx:490
TString fPattern
Definition: TPRegexp.h:46
TPRegexp & operator=(const TPRegexp &p)
Assignment operator.
Definition: TPRegexp.cxx:92
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
Definition: TPRegexp.cxx:135
UInt_t fPCREOpts
Definition: TPRegexp.h:48
PCREPriv_t * fPriv
Definition: TPRegexp.h:47
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
Bool_t MatchB(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Definition: TPRegexp.h:78
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
TString GetPattern() const
Definition: TPRegexp.h:85
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 Bool_t fgThrowAtCompileError
Definition: TPRegexp.h:50
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
virtual ~TPRegexp()
Cleanup.
Definition: TPRegexp.cxx:80
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
TString GetModifiers() const
Return PCRE modifier options as string.
Definition: TPRegexp.cxx:180
static Bool_t GetThrowAtCompileError()
Get value of static flag controlling whether exception should be thrown upon an error during regular ...
Definition: TPRegexp.cxx:499
void Optimize()
Send the pattern through the optimizer.
Definition: TPRegexp.cxx:230
@ kPCRE_GLOBAL
Definition: TPRegexp.h:40
@ kPCRE_OPTIMIZE
Definition: TPRegexp.h:41
@ kPCRE_DEBUG_MSGS
Definition: TPRegexp.h:42
@ kPCRE_INTMASK
Definition: TPRegexp.h:43
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
Provides iteration through tokens of a given string.
Definition: TPRegexp.h:143
virtual ~TStringToken()
Definition: TPRegexp.h:153
Bool_t AtEnd() const
Definition: TPRegexp.h:156
TStringToken(const TString &fullStr, const TString &splitRe, Bool_t retVoid=kFALSE)
Constructor.
Definition: TPRegexp.cxx:965
TPRegexp fSplitRe
Definition: TPRegexp.h:147
Int_t fPos
Definition: TPRegexp.h:149
const TString fFullStr
Definition: TPRegexp.h:146
Bool_t NextToken()
Get the next token, it is stored in this TString.
Definition: TPRegexp.cxx:977
Bool_t fReturnVoid
Definition: TPRegexp.h:148
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
static constexpr double nm
static constexpr double s