Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TPMERegexp Class Reference

Wrapper for PCRE library (Perl Compatible Regular Expressions).

Based on PME - PCRE Made Easy by Zachary Hansen.

Supports main Perl operations using regular expressions (Match, Substitute and Split). To retrieve the results one can simply use operator[] returning a TString.

See regexp_pme.C for example.

Definition at line 97 of file TPRegexp.h.

Public Member Functions

 TPMERegexp ()
 Default constructor. This regexp will match an empty string.
 TPMERegexp (const TPMERegexp &r)
 Copy constructor.
 TPMERegexp (const TString &s, const TString &opts="", Int_t nMatchMax=10)
 Constructor.
 TPMERegexp (const TString &s, UInt_t opts, Int_t nMatchMax=10)
 Constructor.
virtual ~TPMERegexp ()
void AssignGlobalState (const TPMERegexp &re)
 Copy global-match state from 're; so that this regexp can continue parsing the string from where 're' left off.
Int_t GetGlobalPosition () const
Int_t GetNMaxMatches () const
TClassIsA () const override
Int_t Match (const TString &s, UInt_t start=0)
 Runs a match on s against the regex 'this' was created with.
Int_t NMatches () const
TString operator[] (Int_t)
 Returns the sub-string from the internal fMarkers vector.
virtual void Print (Option_t *option="")
 Print the regular expression and modifier options.
void Reset (const TString &s, const TString &opts="", Int_t nMatchMax=-1)
 Reset the pattern and options.
void Reset (const TString &s, UInt_t opts, Int_t nMatchMax=-1)
 Reset the pattern and options.
void ResetGlobalState ()
 Reset state of global match.
void SetNMaxMatches (Int_t nm)
Int_t Split (const TString &s, Int_t maxfields=0)
 Splits into at most maxfields.
void Streamer (TBuffer &) override
void StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b)
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 true (default).

Static Public Member Functions

static TClassClass ()
static const char * Class_Name ()
static constexpr Version_t Class_Version ()
static const char * DeclFileName ()

Protected Types

enum  { kPCRE_GLOBAL = 0x80000000 , kPCRE_OPTIMIZE = 0x40000000 , kPCRE_DEBUG_MSGS = 0x20000000 , kPCRE_INTMASK = 0x0FFF }

Protected Member Functions

void Compile ()
 Compile the fPattern.
TString GetModifiers () const
 Return PCRE modifier options as string.
TString GetPattern () const
Bool_t IsValid () const
 Returns true if underlying PCRE structure has been successfully generated via regexp compilation.
Int_t Match (const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos=nullptr)
 The number of matches is returned, this equals the full match + sub-pattern matches.
Bool_t MatchB (const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Int_t MatchInternal (const TString &s, Int_t start, Int_t nMaxMatch, TArrayI *pos=nullptr) const
 Perform the actual matching - protected method.
TObjArrayMatchS (const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
 Returns a TObjArray of matched substrings as TObjString's.
void Optimize ()
 Send the pattern through the optimizer.
UInt_t ParseMods (const TString &mods) const
 Translate Perl modifier flags into pcre flags.
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 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.
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.

Static Protected Member Functions

static 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.
static 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.

Protected Attributes

void * fAddressOfLastString
Int_t fLastGlobalPosition
TString fLastStringMatched
TArrayI fMarkers
Int_t fNMatches
Int_t fNMaxMatches
TString fPattern
UInt_t fPCREOpts
PCREPriv_tfPriv

Static Protected Attributes

static Bool_t fgThrowAtCompileError = kFALSE

Private Member Functions

TPMERegexpoperator= (const TPMERegexp &)=delete

#include <TPRegexp.h>

Inheritance diagram for TPMERegexp:
TPRegexp

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protectedinherited
Enumerator
kPCRE_GLOBAL 
kPCRE_OPTIMIZE 
kPCRE_DEBUG_MSGS 
kPCRE_INTMASK 

Definition at line 39 of file TPRegexp.h.

Constructor & Destructor Documentation

◆ TPMERegexp() [1/4]

TPMERegexp::TPMERegexp ( )

Default constructor. This regexp will match an empty string.

Definition at line 675 of file TPRegexp.cxx.

◆ TPMERegexp() [2/4]

TPMERegexp::TPMERegexp ( const TString & s,
const TString & opts = "",
Int_t nMatchMax = 10 )

Constructor.

Parameters
[in]sstring to compile into regular expression
[in]optsperl-style character flags to be set on TPME object
[in]nMatchMaxmaximum number of matches

Definition at line 692 of file TPRegexp.cxx.

◆ TPMERegexp() [3/4]

TPMERegexp::TPMERegexp ( const TString & s,
UInt_t opts,
Int_t nMatchMax = 10 )

Constructor.

Parameters
[in]sstring to compile into regular expression
[in]optsPCRE-style option flags to be set on TPME object
[in]nMatchMaxmaximum number of matches

Definition at line 710 of file TPRegexp.cxx.

◆ TPMERegexp() [4/4]

TPMERegexp::TPMERegexp ( const TPMERegexp & r)

Copy constructor.

Only PCRE specifics are copied, not last-match or global-match information.

Definition at line 726 of file TPRegexp.cxx.

◆ ~TPMERegexp()

virtual TPMERegexp::~TPMERegexp ( )
inlinevirtual

Definition at line 118 of file TPRegexp.h.

Member Function Documentation

◆ AssignGlobalState()

void TPMERegexp::AssignGlobalState ( const TPMERegexp & re)

Copy global-match state from 're; so that this regexp can continue parsing the string from where 're' left off.

Alternatively, GetGlobalPosition() get be used to retrieve the last match position so that it can passed to Match().

Ideally, as it is done in PERL, the last match position would be stored in the TString itself.

Definition at line 771 of file TPRegexp.cxx.

◆ Class()

TClass * TPMERegexp::Class ( )
static
Returns
TClass describing this class

◆ Class_Name()

const char * TPMERegexp::Class_Name ( )
static
Returns
Name of this class

◆ Class_Version()

constexpr Version_t TPMERegexp::Class_Version ( )
inlinestaticconstexpr
Returns
Version of this class

Definition at line 139 of file TPRegexp.h.

◆ Compile()

void TPRegexp::Compile ( )
protectedinherited

Compile the fPattern.

Definition at line 225 of file TPRegexp.cxx.

◆ DeclFileName()

const char * TPMERegexp::DeclFileName ( )
inlinestatic
Returns
Name of the file containing the class declaration

Definition at line 139 of file TPRegexp.h.

◆ GetGlobalPosition()

Int_t TPMERegexp::GetGlobalPosition ( ) const
inline

Definition at line 126 of file TPRegexp.h.

◆ GetModifiers()

TString TPRegexp::GetModifiers ( ) const
inherited

Return PCRE modifier options as string.

For meaning of mods see ParseMods().

Definition at line 207 of file TPRegexp.cxx.

◆ GetNMaxMatches()

Int_t TPMERegexp::GetNMaxMatches ( ) const
inline

Definition at line 123 of file TPRegexp.h.

◆ GetPattern()

TString TPRegexp::GetPattern ( ) const
inlineinherited

Definition at line 85 of file TPRegexp.h.

◆ GetThrowAtCompileError()

Bool_t TPRegexp::GetThrowAtCompileError ( )
staticinherited

Get value of static flag controlling whether exception should be thrown upon an error during regular expression compilation by the PCRE engine.

Definition at line 587 of file TPRegexp.cxx.

◆ IsA()

TClass * TPMERegexp::IsA ( ) const
inlineoverridevirtual
Returns
TClass describing current object

Reimplemented from TPRegexp.

Definition at line 139 of file TPRegexp.h.

◆ IsValid()

Bool_t TPRegexp::IsValid ( ) const
inherited

Returns true if underlying PCRE structure has been successfully generated via regexp compilation.

Definition at line 578 of file TPRegexp.cxx.

◆ Match() [1/2]

Int_t TPMERegexp::Match ( const TString & s,
UInt_t start = 0 )

Runs a match on s against the regex 'this' was created with.

Parameters
[in]sstring to match against
[in]startoffset at which to start matching
Returns
number of matches found

Definition at line 795 of file TPRegexp.cxx.

◆ Match() [2/2]

Int_t TPRegexp::Match ( const TString & s,
const TString & mods = "",
Int_t start = 0,
Int_t nMaxMatch = 10,
TArrayI * pos = nullptr )
inherited

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().

Definition at line 409 of file TPRegexp.cxx.

◆ MatchB()

Bool_t TPRegexp::MatchB ( const TString & s,
const TString & mods = "",
Int_t start = 0,
Int_t nMaxMatch = 10 )
inlineinherited

Definition at line 78 of file TPRegexp.h.

◆ MatchInternal()

Int_t TPRegexp::MatchInternal ( const TString & s,
Int_t start,
Int_t nMaxMatch,
TArrayI * pos = nullptr ) const
protectedinherited

Perform the actual matching - protected method.

Definition at line 355 of file TPRegexp.cxx.

◆ MatchS()

TObjArray * TPRegexp::MatchS ( const TString & s,
const TString & mods = "",
Int_t start = 0,
Int_t nMaxMatch = 10 )
inherited

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 sub-patterns. 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();
std::cout << "\"" << subStr << "\" ";
}
std::cout << subStr << std::endl;
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
An array of TObjects.
Definition TObjArray.h:31
TObject * At(Int_t idx) const override
Definition TObjArray.h:170
Int_t GetLast() const override
Return index of last object in array.
Collectable string class.
Definition TObjString.h:28
TPRegexp()
Default ctor.
Definition TPRegexp.cxx:67
Basic string class.
Definition TString.h:138

produces: "abc" "a" "" "bc"

For meaning of mods see ParseMods().

Definition at line 440 of file TPRegexp.cxx.

◆ NMatches()

Int_t TPMERegexp::NMatches ( ) const
inline

Definition at line 134 of file TPRegexp.h.

◆ operator=()

TPMERegexp & TPMERegexp::operator= ( const TPMERegexp & )
privatedelete

◆ operator[]()

TString TPMERegexp::operator[] ( Int_t index)

Returns the sub-string from the internal fMarkers vector.

Requires having run match or split first.

Definition at line 1005 of file TPRegexp.cxx.

◆ Optimize()

void TPRegexp::Optimize ( )
protectedinherited

Send the pattern through the optimizer.

Definition at line 276 of file TPRegexp.cxx.

◆ ParseMods()

UInt_t TPRegexp::ParseMods ( const TString & modStr) const
protectedinherited

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.

Definition at line 162 of file TPRegexp.cxx.

◆ Print()

void TPMERegexp::Print ( Option_t * option = "")
virtual

Print the regular expression and modifier options.

If 'option' contains "all", prints also last string match and match results.

Definition at line 1020 of file TPRegexp.cxx.

◆ ReplaceSubs()

Int_t TPRegexp::ReplaceSubs ( const TString & s,
TString & final,
const TString & replacePattern,
Int_t * ovec,
Int_t nmatch ) const
protectedinherited

Returns the number of expanded '$' constructs.

Definition at line 299 of file TPRegexp.cxx.

◆ Reset() [1/2]

void TPMERegexp::Reset ( const TString & s,
const TString & opts = "",
Int_t nMatchMax = -1 )

Reset the pattern and options.

If 'nMatchMax' other than -1 (the default) is passed, it is also set.

Definition at line 740 of file TPRegexp.cxx.

◆ Reset() [2/2]

void TPMERegexp::Reset ( const TString & s,
UInt_t opts,
Int_t nMatchMax = -1 )

Reset the pattern and options.

If 'nMatchMax' other than -1 (the default) is passed, it is also set.

Definition at line 749 of file TPRegexp.cxx.

◆ ResetGlobalState()

void TPMERegexp::ResetGlobalState ( )

Reset state of global match.

This happens automatically when a new string is passed for matching. But be carefull, as the address of last TString object is used to make this decision.

Definition at line 783 of file TPRegexp.cxx.

◆ SetNMaxMatches()

void TPMERegexp::SetNMaxMatches ( Int_t nm)
inline

Definition at line 124 of file TPRegexp.h.

◆ SetThrowAtCompileError()

void TPRegexp::SetThrowAtCompileError ( Bool_t throwp)
staticinherited

Set static flag controlling whether exception should be thrown upon an error during regular expression compilation by the PCRE engine.

Definition at line 596 of file TPRegexp.cxx.

◆ Split()

Int_t TPMERegexp::Split ( const TString & s,
Int_t maxfields = 0 )

Splits into at most maxfields.

If maxfields is unspecified or 0, trailing empty matches are discarded. If maxfields is positive, no more than maxfields fields will be returned and trailing empty matches are preserved. If maxfields is empty, all fields (including trailing empty ones) are returned. This should be the same as the perl behaviour.

If pattern produces sub-matches, these are also stored in the result.

A pattern matching the null string will split the value of EXPR into separate characters at each point it matches that way.

Parameters
[in]sstring to split
[in]maxfieldsmaximum number of fields to be split out. 0 means split all fields, but discard any trailing empty bits. Negative means split all fields and keep trailing empty bits. Positive means keep up to N fields including any empty fields less than N. Anything remaining is in the last field.
Returns
number of fields found

Definition at line 852 of file TPRegexp.cxx.

◆ Streamer()

void TPMERegexp::Streamer ( TBuffer & )
overridevirtual

Reimplemented from TPRegexp.

◆ StreamerNVirtual()

void TPMERegexp::StreamerNVirtual ( TBuffer & ClassDef_StreamerNVirtual_b)
inline

Definition at line 139 of file TPRegexp.h.

◆ Substitute() [1/2]

Int_t TPMERegexp::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 true (default).

Returns the number of substitutions made.

After the substitution, another pass is made over the resulting string and the following special tokens are interpreted:

  • \l lowercase next char,
  • \u uppercase next char,
  • \L lowercase till \E,
  • \U uppercase till \E, and
  • \E end case modification.

Definition at line 961 of file TPRegexp.cxx.

◆ Substitute() [2/2]

Int_t TPRegexp::Substitute ( TString & s,
const TString & replacePattern,
const TString & mods = "",
Int_t start = 0,
Int_t nMaxMatch = 10 )
inherited

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");
std::cout << nrSub << " \"" << s << "\"" <<std::endl;

produces: 2 "mies noot aap"

For meaning of mods see ParseMods().

Definition at line 560 of file TPRegexp.cxx.

◆ SubstituteInternal()

Int_t TPRegexp::SubstituteInternal ( TString & s,
const TString & replacePattern,
Int_t start,
Int_t nMaxMatch,
Bool_t doDollarSubst ) const
protectedinherited

Perform pattern substitution with optional back-ref replacement.

  • protected method.

Definition at line 466 of file TPRegexp.cxx.

Member Data Documentation

◆ fAddressOfLastString

void* TPMERegexp::fAddressOfLastString
protected

Definition at line 108 of file TPRegexp.h.

◆ fgThrowAtCompileError

Bool_t TPRegexp::fgThrowAtCompileError = kFALSE
staticprotectedinherited

Definition at line 50 of file TPRegexp.h.

◆ fLastGlobalPosition

Int_t TPMERegexp::fLastGlobalPosition
protected

Definition at line 110 of file TPRegexp.h.

◆ fLastStringMatched

TString TPMERegexp::fLastStringMatched
protected

Definition at line 107 of file TPRegexp.h.

◆ fMarkers

TArrayI TPMERegexp::fMarkers
protected

Definition at line 105 of file TPRegexp.h.

◆ fNMatches

Int_t TPMERegexp::fNMatches
protected

Definition at line 104 of file TPRegexp.h.

◆ fNMaxMatches

Int_t TPMERegexp::fNMaxMatches
protected

Definition at line 103 of file TPRegexp.h.

◆ fPattern

TString TPRegexp::fPattern
protectedinherited

Definition at line 46 of file TPRegexp.h.

◆ fPCREOpts

UInt_t TPRegexp::fPCREOpts
protectedinherited

Definition at line 48 of file TPRegexp.h.

◆ fPriv

PCREPriv_t* TPRegexp::fPriv
protectedinherited

Definition at line 47 of file TPRegexp.h.


The documentation for this class was generated from the following files: