ROOT logo
ROOT » CORE » BASE » TPRegexp

class TPRegexp


TPRegexp

C++ Wrapper for the "Perl Compatible Regular Expressions" library
The PCRE lib can be found at:
http://www.pcre.org/

Extensive documentation about Regular expressions in Perl can be
found at :
http://perldoc.perl.org/perlre.html


Function Members (Methods)

public:
TPRegexp()
TPRegexp(const TString& pat)
TPRegexp(const TPRegexp& p)
virtual~TPRegexp()
static TClass*Class()
TStringGetModifiers() const
TStringGetPattern() const
static Bool_tGetThrowAtCompileError()
virtual TClass*IsA() const
Bool_tIsValid() const
Int_tMatch(const TString& s, const TString& mods = "", Int_t start = 0, Int_t nMaxMatch = 10, TArrayI* pos = 0)
Bool_tMatchB(const TString& s, const TString& mods = "", Int_t start = 0, Int_t nMaxMatch = 10)
TObjArray*MatchS(const TString& s, const TString& mods = "", Int_t start = 0, Int_t nMaxMatch = 10)
TPRegexp&operator=(const TPRegexp& p)
static voidSetThrowAtCompileError(Bool_t throwp)
virtual voidShowMembers(TMemberInspector&)
virtual voidStreamer(TBuffer&)
voidStreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b)
Int_tSubstitute(TString& s, const TString& replace, const TString& mods = "", Int_t start = 0, Int_t nMatchMax = 10)
protected:
voidCompile()
Int_tMatchInternal(const TString& s, Int_t start, Int_t nMaxMatch, TArrayI* pos = 0)
voidOptimize()
UInt_tParseMods(const TString& mods) const
Int_tReplaceSubs(const TString& s, TString& final, const TString& replacePattern, Int_t* ovec, Int_t nmatch) const
Int_tSubstituteInternal(TString& s, const TString& replace, Int_t start, Int_t nMaxMatch0, Bool_t doDollarSubst)

Data Members

public:
enum { kPCRE_GLOBAL
kPCRE_OPTIMIZE
kPCRE_DEBUG_MSGS
kPCRE_INTMASK
};
protected:
UInt_tfPCREOpts
TStringfPattern
PCREPriv_t*fPriv
static Bool_tfgThrowAtCompileError

Class Charts

Inheritance Inherited Members Includes Libraries
Class Charts

Function documentation

TPRegexp()
 Default ctor.
TPRegexp(const TString& pat)
 Create and initialize with pat.
TPRegexp(const TPRegexp& p)
 Copy ctor.
~TPRegexp()
 Cleanup.
TPRegexp & operator=(const TPRegexp& p)
 Assignement operator.
UInt_t ParseMods(const TString& mods) const
 Translate Perl modifier flags into pcre flags.
 The supported modStr characters are: g, i, m, o, s, x, and the
 special d for debug. The meaning of the letters is:
 - m
   Treat string as multiple lines. That is, change "^" and "$" from
   matching the start or end of the string to matching the start or
   end of any line anywhere within the string.
 - s
   Treat string as single line. That is, change "." to match any
   character whatsoever, even a newline, which normally it would not match.
   Used together, as /ms, they let the "." match any character whatsoever,
   while still allowing "^" and "$" to match, respectively, just after and
   just before newlines within the string.
 - i
   Do case-insensitive pattern matching.
 - x
   Extend your pattern's legibility by permitting whitespace and comments.
 - p
   Preserve the string matched such that ${^PREMATCH}, ${^MATCH},
   and ${^POSTMATCH} are available for use after matching.
 - g and c
   Global matching, and keep the Current position after failed matching.
   Unlike i, m, s and x, these two flags affect the way the regex is used
   rather than the regex itself. See Using regular expressions in Perl in
   perlretut for further explanation of the g and c modifiers.
 For more detail see: http://perldoc.perl.org/perlre.html#Modifiers.
TString GetModifiers() const
 Return PCRE modifier options as string.
 For meaning of mods see ParseMods().
void Compile()
void Optimize()
 Send the pattern through the optimizer.
Int_t ReplaceSubs(const TString& s, TString& final, const TString& replacePattern, Int_t* ovec, Int_t nmatch) const
 Returns the number of expanded '$' constructs.
Int_t MatchInternal(const TString& s, Int_t start, Int_t nMaxMatch, TArrayI* pos = 0)
 Perform the actual matching - protected method.
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.
 nMaxMatch is the maximum allowed number of matches.
 pos contains the string indices of the matches. Its usage is
 shown in the routine MatchS.
 For meaning of mods see ParseMods().
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.
 The TObjArray is owner of the objects and must be deleted by the user.
 The first entry is the full matched pattern, followed by the subpatterns.
 If a pattern was not matched, it will return an empty substring:

 TObjArray *subStrL = TPRegexp("(a|(z))(bc)").MatchS("abc");
 for (Int_t i = 0; i < subStrL->GetLast()+1; i++) {
    const TString subStr = ((TObjString *)subStrL->At(i))->GetString();
    cout << "\"" << subStr << "\" ";
 }
 cout << subStr << endl;

 produces:  "abc" "a" "" "bc"
 For meaning of mods see ParseMods().
Int_t SubstituteInternal(TString& s, const TString& replace, Int_t start, Int_t nMaxMatch0, Bool_t doDollarSubst)
 Perform pattern substitution with optional back-ref replacement
 - protected method.
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 replacePattern string. The number of
 substitutions are returned.

 TString s("aap noot mies");
 const Int_t nrSub = TPRegexp("(\\w*) noot (\\w*)").Substitute(s,"$2 noot $1");
 cout << nrSub << " \"" << s << "\"" <<endl;

 produces: 2 "mies noot aap"
 For meaning of mods see ParseMods().
Bool_t IsValid() const
 Returns true if underlying PCRE structure has been successfully
 generated via regexp compilation.
Bool_t GetThrowAtCompileError()
 Get value of static flag controlling whether exception should be thrown upon an
 error during regular expression compilation by the PCRE engine.
void SetThrowAtCompileError(Bool_t throwp)
 Set static flag controlling whether exception should be thrown upon an
 error during regular expression compilation by the PCRE engine.
Bool_t MatchB(const TString& s, const TString& mods = "", Int_t start = 0, Int_t nMaxMatch = 10)
TString GetPattern() const
{ return fPattern; }