1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
*
* This file is part of the PCRE++ Class Library.
*
* By accessing this software, PCRE++, you are duly informed
* of and agree to be bound by the conditions described below
* in this notice:
*
* This software product, PCRE++, is developed by Thomas Linden
* and copyrighted (C) 2002 by Thomas Linden, with all rights
* reserved.
*
* There is no charge for PCRE++ software. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General
* Public License, which is incorporated by reference herein.
*
* PCRE++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,
* OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that
* the use of it will not infringe on any third party's intellec-
* tual property rights.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PCRE++. Copies can also be obtained from:
*
* http://www.gnu.org/licenses/lgpl.txt
*
* or by writing to:
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307
* USA
*
* Or contact:
*
* "Thomas Linden" <tom@daemon.de>
*
*
*/
/* you need to include the pcre++ header file */
#include "../libpcre++/pcre++.h"
#include <iostream>
using namespace std;
using namespace pcrepp;
/* A typedef for a vector of strings (as returned by split() )*/
typedef std::vector<std::string> Array;
/* A typedef for a vector iterator */
typedef std::vector<std::string>::iterator ArrayIterator;
void regex() {
/*
* define a string with a regular expression
*/
string expression = "([a-z]*) ([0-9]+)";
/*
* this is the string in which we want to search
*/
string stuff = "hallo 11 robert";
cout << " searching in \"" << stuff << "\" for regex \""
<< expression << "\":" << endl;
/*
* Create a new Pcre object, search case-insensitive ("i")
*/
Pcre reg(expression, "i");
/*
* see if the expression matched
*/
if(reg.search(stuff) == true) {
/*
* see if the expression generated any substrings
*/
if(reg.matches() >= 1) {
/*
* print out the number of substrings
*/
cout << " generated " << reg.matches() << " substrings:" << endl;
/*
* iterate over the matched sub strings
*/
for(int pos=0; pos < reg.matches(); pos++) {
/* print out each substring */
cout << " substring " << pos << ": " << reg[pos]; // also possible: reg.get_match(pos);
/* print out the start/end offset of the current substring
* within the searched string(stuff)
*/
cout << " (start: " << reg.get_match_start(pos) << ", end: "
<< reg.get_match_end(pos) << ")" << endl;
}
}
else {
/*
* we had a match, but it generated no substrings, for whatever reason
*/
cout << " it matched, but there where no substrings." << endl;
}
}
else {
/*
* no match at all
*/
cout << " didn't match." << endl;
}
}
void replace() {
/*
* Sample of replace() usage
*/
string orig = "Hans ist 22 Jahre alt. Er ist 8 Jahre lter als Fred.";
cout << " orig: " << orig << endl;
/*
* define a regex for digits (character class)
*/
Pcre p(" ([0-9]+) ");
/*
* replace the 1st occurence of [0-9]+ with "zweiundzwanzig"
*/
string n = p.replace(orig, " zweiundzwanzig($1) ");
/*
* prints out: "Hans ist zweiundzwanzig Jahre alt. Er ist 8 Jahre lter
* als Fred."
*/
cout << " new: " << n << endl;
}
void replace_multi() {
/*
* Sample of replace() usage with multiple substrings
*/
string orig = " 08:23 ";
cout << " orig: " << orig << endl;
/*
* create regex which, if it matches, creates 3 substrings
*/
Pcre reg(" ([0-9]+)(:)([0-9]+) ", "sig");
/*
* remove $2 (":")
* re-use $1 ("08") and $3 ("23") in the replace string
*/
string n = reg.replace(orig, "$1 Stunden und $3 Minuten");
/*
* prints the result: "08 Stunden und 23 Minuten"
*/
cout << " new: " << n << endl;
}
void normalize() {
/*
* another sample to check if normalizing using replace() works
*/
string orig = "Heute ist ein schoener Tag gell?";
cout << " orig: " << orig << endl;
/*
* create regex for normalizing whitespace
*/
Pcre reg("[\\s]+", "gs");
/*
* do the normalizing process
*/
string n = reg.replace(orig, " ");
/*
* prints the result, should be: "Heute ist ein schoener Tag , gell?"
*/
cout << " new: " << n << endl;
}
void split() {
/*
* Sample of split() usage
*/
string sp_orig = "was21willst2387461du3alter!";
cout << " orig: " << sp_orig << endl;
/*
* define a regex for digits (character class)
*/
string delimiter = "[0-9]+";
/*
* new Pcre object, match globally ("g" flag)
*/
Pcre S(delimiter, "g");
/*
* split "was21willst2387461du3alter!" by digits
*/
Array splitted = S.split(sp_orig);
/*
* iterate over the resulting list
*/
cout << " splitted: ";
for(ArrayIterator A = splitted.begin(); A != splitted.end(); ++A)
cout << *A << " ";
cout << endl;
}
void ex() {
/*
* Pcre::exception Test
*/
/*
* this will generate only one substring, "This"
*/
Pcre ex("([a-z]+)", "i");
if(ex.search("This is a test.")) {
cout << " trying to access a non-existing substring:" << endl;
cout << " substring 2: " << ex.get_match(1) << endl;
}
}
void mycopy() {
/*
* Sample use of copy contsructor and operator=
*/
cout << " initializing reg1(([a-z]+?)" << endl;
Pcre reg1("^([a-z]+?)");
/*
* create an empty Pcre objects
*/
Pcre reg2;
/*
* copy reg1 to reg2 (operator=)
*/
cout << " copying reg1 to new Pcre object reg2" << endl;
reg2 = reg1;
/*
* using the copy constructor to initialize the 3rd object
*/
cout << " creating a new Pcre object reg3 from reg2" << endl;
Pcre reg3(reg2);
/*
* doing regular stuff on reg3
*/
if(reg3.search("anton"))
cout << " string 'anton' matched using reg3 object" << endl;
}
void multisearch() {
Pcre reg("([^\\n]+\\n)");
string str = "\nline1\nline2\nline3\n";
size_t pos = 0;
while (pos <= str.length()) {
if( reg.search(str, pos)) {
pos = reg.get_match_end(0);
cout << " pos: " << pos << " match: " << reg.get_match(0);
}
else
break;
}
}
int main() {
/*
* the Pcre class throws errors via exceptions
*/
try {
cout << endl << "SEARCH() sample:" << endl;
regex();
cout << endl << "REPLACE() sample:" << endl;
replace();
cout << endl << "Multiple REPLACE() sample:" << endl;
replace_multi();
cout << endl << "Normalizing REPLACE() sample:" << endl;
normalize();
cout << endl << "SPLIT() sample:" << endl;
split();
cout << endl << "COPY+Operator sample:" << endl;
mycopy();
cout << endl << "Multi line search test:" << endl;
multisearch();
cout << endl << "Pcre::exception test:" << endl;
ex();
exit(0);
}
catch (Pcre::exception &E) {
/*
* the Pcre class has thrown an exception
*/
cerr << "Pcre++ error: " << E.what() << endl;
exit(-1);
}
exit(0);
}
|