#include <pcre++.h>
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) |
Array * | get_sub_strings () |
string | get_match (int pos) |
int | get_match_start (int pos) |
int | get_match_end (int pos) |
size_t | get_match_length (int pos) |
bool | matched () |
int | matches () |
Array | split (const string &piece) |
Array | split (const string &piece, int limit) |
Array | split (const string &piece, int limit, int start_offset) |
Array | split (const string &piece, int limit, int start_offset, int end_offset) |
Array | split (const string &piece, vector< int > positions) |
string | replace (const string &piece, const string &with) |
Public Attributes | |
bool | did_match |
int | num_matches |
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> 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.num_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.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"); /* * prints out: "Hans ist zweiundzwanzig Jahre alt. Er ist 8 Jahre älter als Fred." */ 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; } } int main() { /* * the Pcre class throws errors via exceptions */ try { cout << endl << "SEARCH() sample:" << endl; regex(); cout << endl << "REPLACE() sample:" << endl; replace(); cout << endl << "SPLIT() sample:" << endl; split(); 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); }
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. Compile the given pattern. An Pcre object created this way can be used multiple times to do searches.
Definition at line 45 of file pcre++.cc. Referenced by operator=(). |
|
Constructor. Compile the given pattern. An Pcre object created this way can be used multiple times to do searches.
|
|
Copy Constructor Creates a new Pcre object of an existing one.
Definition at line 71 of file pcre++.cc. References _expression, _flags, case_t, did_match, global_t, and num_matches. |
|
Destructor. The desturcor will automatically invoked if the object is no more used. It frees all the memory allocated by pcre++. Definition at line 111 of file pcre++.cc. References did_match, and num_matches. |
|
Get a substring at a known position. This method throws an out-of-range exception if the given position is invalid.
string mysub = regex.get_match(1); Definition at line 184 of file pcre++.cc. References ArrayIterator, and num_matches. |
|
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.
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.
Definition at line 206 of file pcre++.cc. References num_matches. Referenced by replace(). |
|
Get the length of a substring at a known position. This method throws an out-of-range exception if the given position is invalid.
Definition at line 220 of file pcre++.cc. References Array, ArrayIterator, and num_matches. |
|
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.
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.
Definition at line 194 of file pcre++.cc. References num_matches. Referenced by replace(). |
|
Return a vector of substrings, if any.
Definition at line 177 of file pcre++.cc. References Array. |
|
Test if a search was successfull. This method must be invoked after calling search().
Definition at line 319 of file pcre++.h. References did_match. Referenced by replace(). |
|
Get the number of substrings generated by pcre++.
Definition at line 324 of file pcre++.h. References Array, and num_matches. Referenced by replace(). |
|
Operator =.
Pcre regex = "(A+?)"; @codeend; Definition at line 105 of file pcre++.cc. References Pcre(). |
|
Replace parts of a string using regular expressions. This method is the counterpart of the perl s/// operator. It replaces the substrings which matched the given regular expression (given to the constructor) with the supplied string.
Definition at line 340 of file pcre++.cc. References Array, get_match_end(), get_match_start(), matched(), matches(), num_matches, search(), and split(). |
|
Do a search on the given string beginning at the given offset. This method does the actual search on the given string.
|
|
Do a search on the given string. This method does the actual search on the given string.
Definition at line 128 of file pcre++.cc. References Array, did_match, and num_matches. Referenced by replace(). |
|
split a string into pieces This method will split the given string into a vector of strings using the compiled expression (given to the constructor).
Definition at line 330 of file pcre++.cc. References Array. |
|
split a string into pieces This method will split the given string into a vector of strings using the compiled expression (given to the constructor).
Definition at line 326 of file pcre++.cc. References Array. |
|
split a string into pieces This method will split the given string into a vector of strings using the compiled expression (given to the constructor).
Definition at line 322 of file pcre++.cc. References Array. |
|
split a string into pieces This method will split the given string into a vector of strings using the compiled expression (given to the constructor).
Definition at line 318 of file pcre++.cc. References Array. |
|
split a string into pieces This method will split the given string into a vector of strings using the compiled expression (given to the constructor).
Definition at line 314 of file pcre++.cc. References Array. Referenced by replace(). |
|
|
|
true if the expression produced a match Definition at line 160 of file pcre++.h. Referenced by get_match(), get_match_end(), get_match_length(), get_match_start(), matches(), Pcre(), replace(), search(), and ~Pcre(). |