143 const char *
m = modStr;
150 opts |= PCRE_CASELESS;
153 opts |= PCRE_MULTILINE;
162 opts |= PCRE_EXTENDED;
168 Error(
"ParseMods",
"illegal pattern modifier: %c", *
m);
185 if (
fPCREOpts & PCRE_CASELESS) ret +=
'i';
186 if (
fPCREOpts & PCRE_MULTILINE) ret +=
'm';
188 if (
fPCREOpts & PCRE_EXTENDED) ret +=
'x';
209 &errstr, &patIndex,
nullptr);
213 throw std::runtime_error
214 (
TString::Format(
"TPRegexp::Compile() compilation of TPRegexp(%s) failed at: %d because %s",
217 Error(
"Compile",
"compilation of TPRegexp(%s) failed at: %d because %s",
243 Error(
"Optimize",
"Optimization of TPRegexp(%s) failed: %s",
256 const char *p = replacePattern;
260 while (state != -1) {
274 }
else if (!isdigit(p[1])) {
275 Error(
"ReplaceSubs",
"badly formed replacement pattern: %s",
276 replacePattern.
Data());
287 Info(
"ReplaceSubs",
"PREGEX appending substr #%d", subnum);
288 if (subnum < 0 || subnum > nrMatch-1) {
289 Error(
"ReplaceSubs",
"bad string number: %d",subnum);
291 const TString subStr = s(offVec[2*subnum],offVec[2*subnum+1]-offVec[2*subnum]);
314 offVec, 3*nMaxMatch);
316 if (nrMatch == PCRE_ERROR_NOMATCH)
318 else if (nrMatch <= 0) {
319 Error(
"Match",
"pcre_exec error = %d", nrMatch);
325 pos->
Set(2*nrMatch, offVec);
374 Int_t nrMatch =
Match(s, mods, start, nMaxMatch, &pos);
379 for (
Int_t i = 0; i < nrMatch; i++) {
380 Int_t startp = pos[2*i];
381 Int_t stopp = pos[2*i+1];
382 if (startp >= 0 && stopp >= 0) {
383 const TString subStr = s(pos[2*i], pos[2*i+1]-pos[2*i]);
398 Bool_t doDollarSubst)
const
404 Int_t offset = start;
413 offVec, 3*nMaxMatch);
415 if (nrMatch == PCRE_ERROR_NOMATCH) {
417 }
else if (nrMatch <= 0) {
418 Error(
"Substitute",
"pcre_exec error = %d", nrMatch);
423 if (last <= offVec[0]) {
424 fin += s(last,offVec[0]-last);
430 ReplaceSubs(s, fin, replacePattern, offVec, nrMatch);
432 fin += replacePattern;
440 if (offVec[0] != offVec[1]) {
444 if (offVec[1] == s.
Length())
break;
445 offset = offVec[1]+1;
451 fin += s(last,s.
Length()-last);
525 Int_t nrMatch =
r.Match(*
this,
"",start,10,&pos);
540 const Int_t nrMatch =
r.Match(*
this,
"",start,10,&pos);
542 *extent = pos[1]-pos[0];
565 return (*
this)(
r, 0);
590 fAddressOfLastString(nullptr),
591 fLastGlobalPosition(0)
605 fNMaxMatches(nMatchMax),
607 fAddressOfLastString(nullptr),
608 fLastGlobalPosition(0)
623 fNMaxMatches(nMatchMax),
625 fAddressOfLastString(nullptr),
626 fLastGlobalPosition(0)
639 fNMaxMatches(
r.fNMaxMatches),
641 fAddressOfLastString(nullptr),
642 fLastGlobalPosition(0)
765 typedef std::pair<int, int> MarkerLoc_t;
766 typedef std::vector<MarkerLoc_t> MarkerLocVec_t;
769 MarkerLocVec_t oMarks;
776 MarkerLocVec_t oCurrentTrailingEmpties;
779 Int_t nMatchesFound = 0;
785 while ((matchRes =
Match(s, nOffset)) &&
786 ((maxfields < 1) || nMatchesFound < maxfields)) {
790 oMarks.push_back(MarkerLoc_t(nOffset, nOffset + 1));
792 if (nOffset >= s.
Length())
800 if (!oCurrentTrailingEmpties.empty()) {
801 oMarks.insert(oMarks.end(),
802 oCurrentTrailingEmpties.begin(),
803 oCurrentTrailingEmpties.end());
804 oCurrentTrailingEmpties.clear();
806 oMarks.push_back(MarkerLoc_t(nOffset,
fMarkers[0]));
809 if (maxfields == 0) {
811 oCurrentTrailingEmpties.push_back(MarkerLoc_t(nOffset, nOffset));
813 oMarks.push_back(MarkerLoc_t(nOffset, nOffset));
820 for (
Int_t i = 1; i < matchRes; ++i)
827 if (nMatchesFound == 0) {
828 oMarks.push_back(MarkerLoc_t(0, s.
Length()));
832 else if (maxfields > 0 && nMatchesFound >= maxfields) {
833 oMarks[oMarks.size() - 1].second = s.
Length();
838 if (!last_empty || maxfields < 0) {
839 if (!oCurrentTrailingEmpties.empty()) {
840 oMarks.insert(oMarks.end(),
841 oCurrentTrailingEmpties.begin(),
842 oCurrentTrailingEmpties.end());
844 oMarks.push_back(MarkerLoc_t(nOffset, s.
Length()));
852 fMarkers[2*i + 1] = oMarks[i].second;
885 case 0 : ret +=
'\\';
break;
886 case 'l': state = 1;
break;
887 case 'u': state = 2;
break;
888 case 'L': state = 3;
break;
889 case 'U': state = 4;
break;
890 case 'E': state = 0;
break;
891 default : ret +=
'\\'; ret +=
c;
break;
896 case 0: ret +=
c;
break;
897 case 1: ret += (
Char_t) tolower(
c); state = 0;
break;
898 case 2: ret += (
Char_t) toupper(
c); state = 0;
break;
899 case 3: ret += (
Char_t) tolower(
c);
break;
900 case 4: ret += (
Char_t) toupper(
c);
break;
901 default:
Error(
"TPMERegexp::Substitute",
"invalid state.");
941 Printf(
" %d - %s", i,
operator[](i).Data());
966 fReturnVoid (retVoid),
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Printf(const char *fmt,...)
Array of integers (32 bits per element).
void Set(Int_t n)
Set size of this array to n ints.
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Collectable string class.
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Int_t fLastGlobalPosition
void ResetGlobalState()
Reset state of global match.
void * fAddressOfLastString
virtual void Print(Option_t *option="")
Print the regular expression and modifier options.
Int_t Split(const TString &s, Int_t maxfields=0)
Splits into at most maxfields.
TPMERegexp()
Default constructor. This regexp will match an empty string.
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...
TString operator[](Int_t)
Returns the sub-string from the internal fMarkers vector.
Int_t Match(const TString &s, UInt_t start=0)
Runs a match on s against the regex 'this' was created with.
void Reset(const TString &s, const TString &opts="", Int_t nMatchMax=-1)
Reset the pattern and options.
TString fLastStringMatched
void AssignGlobalState(const TPMERegexp &re)
Copy global-match state from 're; so that this regexp can continue parsing the string from where 're'...
void Compile()
Compile the fPattern.
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.
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation.
TPRegexp & operator=(const TPRegexp &p)
Assignment operator.
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
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.
Int_t MatchInternal(const TString &s, Int_t start, Int_t nMaxMatch, TArrayI *pos=0) const
Perform the actual matching - protected method.
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.
static Bool_t fgThrowAtCompileError
Int_t ReplaceSubs(const TString &s, TString &final, const TString &replacePattern, Int_t *ovec, Int_t nmatch) const
Returns the number of expanded '$' constructs.
virtual ~TPRegexp()
Cleanup.
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...
TString GetModifiers() const
Return PCRE modifier options as string.
static Bool_t GetThrowAtCompileError()
Get value of static flag controlling whether exception should be thrown upon an error during regular ...
void Optimize()
Send the pattern through the optimizer.
static void SetThrowAtCompileError(Bool_t throwp)
Set static flag controlling whether exception should be thrown upon an error during regular expressio...
Provides iteration through tokens of a given string.
TStringToken(const TString &fullStr, const TString &splitRe, Bool_t retVoid=kFALSE)
Constructor.
Bool_t NextToken()
Get the next token, it is stored in this TString.
char & operator()(Ssiz_t i)
void ToLower()
Change string to lower-case.
const char * Data() const
TString & operator=(char s)
Assign character c to TString.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
A zero length substring is legal.