41 const TQString & replacementStr,
42 int start,
bool global )
48 TQStringList literalStrs;
49 TQValueList<int> backRefs;
53 TQRegExp rx(
"\\\\(\\d)|\\$(\\d)|\\$\\{(\\d+)\\}" );
54 TQRegExp bbrx(
"\\\\");
58 kdDebug() <<
"Analyzing replacementStr: \"" + replacementStr +
"\"" << endl;
64 pos = rx.search( replacementStr, pos );
67 kdDebug() << TQString(
" Found match at pos %1").arg(pos) << endl;
71 literalStrs << replacementStr.mid( oldpos )
72 .replace( bbrx,
"\\" )
75 kdDebug() <<
" No more matches. Last literal is \"" + literalStrs.last() +
"\"" << endl;
79 literalStrs << replacementStr.mid( oldpos, pos-oldpos )
80 .replace( bbrx,
"\\" )
83 kdDebug() << TQString(
" Inserting \"") + literalStrs.last() +
"\" as literal." << endl;
84 kdDebug() <<
" Searching for corresponding digit(s):" << endl;
86 for (
int i = 1 ; i < 4 ; i++ )
87 if ( !rx.cap(i).isEmpty() ) {
88 backRefs << rx.cap(i).toInt();
90 kdDebug() << TQString(
" Found %1 at position %2 in the capturedTexts.")
91 .arg(backRefs.last()).arg(i) << endl;
95 pos += rx.matchedLength();
97 kdDebug() << TQString(
" Setting new pos to %1.").arg(pos) << endl;
103 #ifdef DEBUG_KREGEXP3
104 kdDebug() <<
"Finished the analysis of replacementStr!" << endl;
106 Q_ASSERT( literalStrs.count() == backRefs.count() + 1 );
110 TQString result =
"";
114 TQStringList::Iterator sIt;
115 TQValueList<int>::Iterator iIt;
118 start += str.length();
120 #ifdef DEBUG_KREGEXP3
121 kdDebug() <<
"Constructing the resultant string starts now:" << endl;
124 while ( pos < (
int)str.length() ) {
125 pos = search( str, pos );
127 #ifdef DEBUG_KREGEXP3
128 kdDebug() << TQString(
" Found match at pos %1").arg(pos) << endl;
132 result += str.mid( oldpos );
133 #ifdef DEBUG_KREGEXP3
134 kdDebug() <<
" No more matches. Adding trailing part from str:" << endl;
135 kdDebug() <<
" result == \"" + result +
"\"" << endl;
139 result += str.mid( oldpos, pos-oldpos );
140 #ifdef DEBUG_KREGEXP3
141 kdDebug() <<
" Adding unchanged part from str:" << endl;
142 kdDebug() <<
" result == \"" + result +
"\"" << endl;
144 for ( sIt = literalStrs.begin(), iIt = backRefs.begin() ;
145 iIt != backRefs.end() ; ++sIt, ++iIt ) {
147 #ifdef DEBUG_KREGEXP3
148 kdDebug() <<
" Adding literal replacement part:" << endl;
149 kdDebug() <<
" result == \"" + result +
"\"" << endl;
151 result += cap( (*iIt) );
152 #ifdef DEBUG_KREGEXP3
153 kdDebug() <<
" Adding captured string:" << endl;
154 kdDebug() <<
" result == \"" + result +
"\"" << endl;
158 #ifdef DEBUG_KREGEXP3
159 kdDebug() <<
" Adding literal replacement part:" << endl;
160 kdDebug() <<
" result == \"" + result +
"\"" << endl;
163 if (matchedLength() == 0 && pos == 0) {
166 result += str.mid( oldpos );
169 pos += matchedLength();
170 #ifdef DEBUG_KREGEXP3
171 kdDebug() << TQString(
" Setting new pos to %1.").arg(pos) << endl;
177 result += str.mid( oldpos );
TQString replace(const TQString &str, const TQString &replacementStr, int start=0, bool global=TRUE)
Replaces each matching subpattern in str with replacementStr, inserting captured substrings for \n,...