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

Pcre Class Reference

#include <pcre++.h>

List of all members.

Public Methods

 Pcre (const string &expression)
 Pcre (const string &expression, const string &flags)
 Pcre (Pcre &P)
const Pcre& operator= (const string &expression)
 ~Pcre ()
bool search (const string &stuff)
bool search (const string &stuff, int OffSet)
ResultSetget_sub_strings ()
string get_match (int pos)
int get_match_start (int pos)
int get_match_end (int pos)
bool matched ()
int matches ()

Public Attributes

bool did_match
int num_matches


Detailed Description

The Pcre class is a wrapper around the PCRE library.

The library "pcre++" defines a class named "Pcre" which you can use to search in strings using reular expressions as well as getting matched sub strings. It does currently not support all features, which the underlying PCRE library provides, but the most important stuff is implemented.

Please study this example code to learn how to use this class:

/* you need to include the pcre++ header file */
#include <pcre++.h>
#include <iostream>

int main() {
  /* 
   * the Pcre class throws errors via exceptions
   */
  try {
    /*
     * 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";

    /*
     * 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.num_matches >= 1) {

        /*
         * print out the number of substrings
         */
        cout << "matched " << reg.matches() << " times:" << endl;
          
        /*
         * iterate over the matched sub strings
         */
        for(int pos=0; pos < reg.matches(); pos++) {
          /* print out each substring */
          cout << "  match " << pos << ": " << reg.get_match(pos) << endl;
          /* 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;
    }
  } /* try {} end */
  catch (Exception E) {
    /*
     * the Pcre class has thrown an exception
     */
    cerr << E.mout() << endl;
    exit(-1);
  }
  exit(0);
}
  

Compile your programs which use the prce++ class using the following LDFLAGS:

   g++ yourcode.o .. -L/path/to/the/lib -lpcrepp -o yourprogram

If you want to learn more about regular expressions which can be used with pcre++, then please read the following documentation: perlre - Perl regular expressions

The pcre library itself does also contain some usefull documentation, which maybe interesting for you: PCRE manual page

Definition at line 92 of file pcre++.h.


Constructor & Destructor Documentation

Pcre::Pcre ( const string & expression )
 

Constructor. Compile the given pattern. An Pcre object created this way can be used multiple times to do searches.

Parameters:
string expression   a string, which must be a valid perl regular expression.
Returns:
A new Pcre object, which holds te compiled pattern.
See also:
Pcre(const string& _expression, const string& flags)

Pcre::Pcre ( const string & _expression,
const string & flags )
 

Constructor. Compile the given pattern. An Pcre object created this way can be used multiple times to do searches.

Parameters:
string expression   a string, which must be a valid perl regular expression.
string flags   can be one or more of the following letters:

  • i Search case insensitive.
  • m Match on multiple lines, thus ^ and $ are interpreted as the start and end of the entire string, not of a single line.
  • s A dot in an expression matches newlines too(which is normally not the case).
  • x Whitespace characters will be ignored (except within character classes or if escaped).
Returns:
A new Pcre object, which holds te compiled pattern.
See also:
Pcre(const string& expression)

Pcre::Pcre ( Pcre & P )
 

Copy Constructor Creates a new Pcre object of an existing one.

Parameters:
Pcre P   an existing Pcre object.
Returns:
A new Pcre object, which holds te compiled pattern.
See also:
Pcre(const string& expression) , Pcre(const string& expression, const string& flags)

Pcre::~Pcre ( )
 

Destructor. The desturcor will automatically invoked if the object is no more used. It frees all the memory allocated by pcre++.


Member Function Documentation

string Pcre::get_match ( int pos )
 

Get a substring at a known position. This method throws an out-of-range exception if the given position is invalid.

Parameters:
int pos   the position of the substring to return. Identical to perl's $1..$n.
Returns:
the substring at the given position.

Example:
 string mysub = regex.get_match(1); 
Get the first substring that metched the expression in the "regex" object.

int Pcre::get_match_end ( int pos )
 

Get the end position of a substring within the searched string. This method returns the character position of the last character of a substring withing the searched string.

Parameters:
int pos   the position of the substring. Identical to perl's $1..$n.
Returns:
the integer character position of the last character of a substring. Positions are starting at 0.

Example:
 Pcre regex("([0-9]+)");               // search for numerical characters
 regex.search("The 11th september.");  // do the search on this string
 string day = regex.get_match(1);      // returns "11"
 int pos = regex.get_match_end(1);     // returns 5, because "11" ends at the
                                       // 5th character inside the search string.
See also:
int get_match_start(int pos)

int Pcre::get_match_start ( int pos )
 

Get the start position of a substring within the searched string. This method returns the character position of the first character of a substring withing the searched string.

Parameters:
int pos   the position of the substring. Identical to perl's $1..$n.
Returns:
the integer character position of the first character of a substring. Positions are starting at 0.

Example:
 Pcre regex("([0-9]+)");               // search for numerical characters
 regex.search("The 11th september.");  // do the search on this string
 string day = regex.get_match(1);      // returns "11"
 int pos = regex.get_match_start(1);   // returns 4, because "11" begins at the
                                       // 4th character inside the search string.
See also:
int get_match_end(int pos)

ResultSet * Pcre::get_sub_strings ( )
 

Return a vector of substrings, if any.

Returns:
a pointer to a ResultSet, which may be NULL, if no substrings has been found.
See also:
ResultSet

bool Pcre::matched ( ) [inline]
 

Test if a search was successfull. This method must be invoked after calling search().

Returns:
boolean true if the search was successfull at all, or false if not.

Definition at line 261 of file pcre++.h.

int Pcre::matches ( ) [inline]
 

Get the number of substrings generated by pcre++.

Returns:
the number of substrings generated by pcre++.

Definition at line 266 of file pcre++.h.

const Pcre & Pcre::operator= ( const string & expression )
 

Operator =.

Parameters:
string expression   a valid regular expression.
Returns:
a new Pcre object.

Example:

 Pcre regex = "(A+?)";
 @codeend;

bool Pcre::search ( const string & stuff,
int OffSet )
 

Do a search on the given string beginning at the given offset. This method does the actual search on the given string.

Parameters:
string stuff   the string in which you want to search for something.
int OffSet   the offset where to start the search.
Returns:
boolean true if the regular expression matched. false if not.
See also:
bool search(const string& stuff)

bool Pcre::search ( const string & stuff )
 

Do a search on the given string. This method does the actual search on the given string.

Parameters:
string stuff   the string in which you want to search for something.
Returns:
boolean true if the regular expression matched. false if not.
See also:
bool search(const string& stuff, int OffSet)


Member Data Documentation

bool Pcre::did_match
 

Definition at line 113 of file pcre++.h.

int Pcre::num_matches
 

true if the expression produced a match

Definition at line 114 of file pcre++.h.


The documentation for this class was generated from the following file:
Generated at Fri Jan 4 03:57:04 2002 for PCRE++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001