00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "pcre++.h"
00043
00044 using namespace std;
00045 using namespace pcrepp;
00046
00047
00048
00049
00050
00051 string Pcre::replace(const string& piece, const string& with) {
00052
00053
00054
00055 string Replaced(piece);
00056
00057 bool bReplaced = false;
00058 int iReplaced = -1;
00059
00060 __pcredebug << "replace: " << piece << " with: " << with << endl;
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 pcre_free(p_pcre);
00073 pcre_free(p_pcre_extra);
00074
00075 pcre *_p = NULL;
00076 pcre_extra *_e = NULL;;
00077
00078 p_pcre = _p;
00079 p_pcre_extra = _e;
00080
00081 if (! _have_paren ) {
00082 string::size_type p_open, p_close;
00083 p_open = _expression.find_first_of("(");
00084 p_close = _expression.find_first_of(")");
00085 if ( p_open == string::npos && p_close == string::npos ) {
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 _expression = "((" + _expression + "))";
00097 }
00098 else {
00099
00100
00101
00102
00103
00104
00105
00106 _expression = "(" + _expression;
00107 _expression=_expression + ")";
00108 }
00109
00110 _have_paren = true;
00111 }
00112
00113 __pcredebug << "_expression: " << _expression << endl;
00114
00115 Compile(_flags);
00116
00117 if(search(piece)) {
00118
00119
00120
00121
00122 string use_with;
00123
00124
00125 if(!global_t) {
00126
00127 use_with = _replace_vars(with);
00128
00129 if(matched() && matches() >= 1) {
00130 __pcredebug << "matches: " << matches() << endl;
00131 int len = get_match_end() - get_match_start() + 1;
00132 Replaced.replace(get_match_start(0), len, use_with);
00133 bReplaced = true;
00134 iReplaced = 0;
00135 }
00136 }
00137 else {
00138
00139
00140
00141
00142 int match_pos = 0;
00143 while( search( Replaced, match_pos ) ) {
00144 int len = 0;
00145
00146
00147
00148 use_with = _replace_vars(with);
00149
00150 len = get_match_end() - get_match_start() + 1;
00151 Replaced.replace(get_match_start(0), len, use_with);
00152
00153
00154 match_pos = ( use_with.length() - len ) + get_match_end() + 1;
00155
00156 bReplaced = true;
00157 ++iReplaced;
00158 }
00159 }
00160 }
00161
00162 did_match = bReplaced;
00163 num_matches = iReplaced;
00164
00165 return Replaced;
00166 }
00167
00168
00169
00170
00171
00172 string Pcre::_replace_vars(const string& piece) {
00173
00174
00175
00176 string with = piece;
00177 Pcre dollar("\\$([0-9]+)");
00178
00179 while ( dollar.search( with ) ) {
00180
00181 __pcredebug << "Pcre::dollar matched: " << piece << ". Match(0): " << dollar.get_match(0) << endl;
00182 int iBracketIndex = atoi( dollar.get_match(0).data() );
00183 string sBracketContent = get_match(iBracketIndex);
00184
00185
00186 string sSubSplit = "\\$" + dollar.get_match(0);
00187 Pcre subsplit(sSubSplit);
00188
00189
00190 vector<string> splitted = subsplit.split(with);
00191 string Replaced;
00192
00193 for(size_t pos=0; pos < splitted.size(); pos++) {
00194 if( pos == ( splitted.size() - 1 ) )
00195 Replaced += splitted[pos];
00196 else
00197 Replaced += splitted[pos] + sBracketContent;
00198 }
00199 with = Replaced;
00200 }
00201 return with;
00202 }