Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Examples  

sample.cc

The Pcre class is a wrapper around the PCRE library.

#include "pcre++.h"
#include <iostream>

int main() {
  /* the Pcre class throws errors via exceptions */
  try {
    string expression = "([a-z]*) ([0-9]+)";

    string stuff[] = { "hallo 11 robert", "so what?", "hey 123" };
    Pcre reg(expression, "i");

    for(int st=0; st < 3; st++) {
      cout << endl << "search on \"" << stuff[st] << "\" ";
      if(reg.search(stuff[st]) == true) {
        if(reg.num_matches > 1) {
          cout << "matched " << reg.matches() << " times:" << endl;
          
          /* iterate directly via the vector<tring> type ResultSet */
          ResultSet *results = reg.get_sub_strings();
          int _pos = 0;
          for(ResultIterator p = results->begin(); p != results->end(); ++p) {
            string match = *p;
            cout << "  Iterator match " << _pos++ << ": " << match << endl;
          }
          
          cout << endl;
          
          /* iterate indirectly via Pcre class interface method */
          for(int pos=0; pos < reg.matches(); pos++) {
            cout << "  Class match " << pos << ": " << reg.get_match(pos) << endl;
            cout << "     start: " << reg.get_match_start(pos) << ", end: " << reg.get_match_end(pos) << endl;
          }
          
        }
        else {
          cout << "it matched, there where no substrings." << endl;
        }
      }
      else {
        cout << "didn't match." << endl;
      }
    } // for {}
  } // try {}
  catch (Exception E) {
    cerr << E.mout() << endl;
    exit(-1);
  }
  exit(0);
}
  

Generated at Thu Jan 3 18:09:14 2002 for PCRE++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001