...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The high level wrapper class RegEx is now deprecated and does not form part of the regular expression standardization proposal. This type still exists, and existing code will continue to compile, however the following documentation is unlikely to be further updated.
#include <boost/cregex.hpp>
The class RegEx provides a high level simplified interface to the regular expression library, this class only handles narrow character strings, and regular expressions always follow the "normal" syntax - that is the same as the perl / ECMAScript synatx.
typedef bool (*GrepCallback)(const RegEx& expression); typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression); typedef bool (*FindFilesCallback)(const char* file); class RegEx { public: RegEx(); RegEx(const RegEx& o); ~RegEx(); RegEx(const char* c, bool icase = false); explicit RegEx(const std::string& s, bool icase = false); RegEx& operator=(const RegEx& o); RegEx& operator=(const char* p); RegEx& operator=(const std::string& s); unsigned int SetExpression(const char* p, bool icase = false); unsigned int SetExpression(const std::string& s, bool icase = false); std::string Expression()const; // // now matching operators: // bool Match(const char* p, boost::match_flag_type flags = match_default); bool Match(const std::string& s, boost::match_flag_type flags = match_default); bool Search(const char* p, boost::match_flag_type flags = match_default); bool Search(const std::string& s, boost::match_flag_type flags = match_default); unsigned int Grep(GrepCallback cb, const char* p, boost::match_flag_type flags = match_default); unsigned int Grep(GrepCallback cb, const std::string& s, boost::match_flag_type flags = match_default); unsigned int Grep(std::vector<std::string>& v, const char* p, boost::match_flag_type flags = match_default); unsigned int Grep(std::vector<std::string>& v, const std::string& s, boost::match_flag_type flags = match_default); unsigned int Grep(std::vector<unsigned int>& v, const char* p, boost::match_flag_type flags = match_default); unsigned int Grep(std::vector<unsigned int>& v, const std::string& s, boost::match_flag_type flags = match_default); unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, boost::match_flag_type flags = match_default); unsigned int GrepFiles(GrepFileCallback cb, const std::string& files, bool recurse = false, boost::match_flag_type flags = match_default); unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, boost::match_flag_type flags = match_default); unsigned int FindFiles(FindFilesCallback cb, const std::string& files, bool recurse = false, boost::match_flag_type flags = match_default); std::string Merge(const std::string& in, const std::string& fmt, bool copy = true, boost::match_flag_type flags = match_default); std::string Merge(const char* in, const char* fmt, bool copy = true, boost::match_flag_type flags = match_default); unsigned Split(std::vector<std::string>& v, std::string& s, boost::match_flag_type flags = match_default, unsigned max_count = ~0); // // now operators for returning what matched in more detail: // unsigned int Position(int i = 0)const; unsigned int Length(int i = 0)const; bool Matched(int i = 0)const; unsigned int Line()const; unsigned int Marks() const; std::string What(int i)const; std::string operator[](int i)const ; static const unsigned int npos; };
Member functions for class RegEx are defined as follows:
Member |
Description |
---|---|
|
Default constructor, constructs an instance of RegEx without any valid expression. |
|
Copy constructor, all the properties of parameter o are copied. |
|
Constructs an instance of RegEx, setting the expression to c,
if icase is true then matching is insensitive
to case, otherwise it is sensitive to case. Throws |
|
Constructs an instance of RegEx, setting the expression to s,
if icase is true then matching is insensitive
to case, otherwise it is sensitive to case. Throws |
|
Default assignment operator. |
|
Assignment operator, equivalent to calling |
|
Assignment operator, equivalent to calling |
|
Sets the current expression to p, if icase
is true then matching is insensitive to case, otherwise it is
sensitive to case. Throws |
|
Sets the current expression to s, if icase
is true then matching is insensitive to case, otherwise it is
sensitive to case. Throws |
|
Returns a copy of the current regular expression. |
|
Attempts to match the current expression against the text p
using the match flags flags - see |
|
Attempts to match the current expression against the text s
using the |
|
Attempts to find a match for the current expression somewhere
in the text p using the |
|
Attempts to find a match for the current expression somewhere
in the text s using the |
|
Finds all matches of the current expression in the text p
using the |
|
Finds all matches of the current expression in the text s
using the |
|
Finds all matches of the current expression in the text p
using the |
|
Finds all matches of the current expression in the text s
using the |
|
Finds all matches of the current expression in the text p
using the |
|
Finds all matches of the current expression in the text s
using the |
|
Finds all matches of the current expression in the files files
using the The parameter files can include wild card characters '*' and '?', if the parameter recurse is true then searches sub-directories for matching file names. Returns the total number of matches found.
May throw an exception derived from |
|
Finds all matches of the current expression in the files files
using the If the call-back returns false then the algorithm returns without considering further matches in the current file, or any further files. The parameter files can include wild card characters '*' and '?', if the parameter recurse is true then searches sub-directories for matching file names. Returns the total number of matches found.
May throw an exception derived from |
|
Searches files to find all those which contain at least one match
of the current expression using the The parameter files can include wild card characters '*' and '?', if the parameter recurse is true then searches sub-directories for matching file names. Returns the total number of files found.
May throw an exception derived from |
|
Searches files to find all those which contain at least one match
of the current expression using the If the call-back returns false then the algorithm returns without considering any further files. The parameter files can include wild card characters '*' and '?', if the parameter recurse is true then searches sub-directories for matching file names. Returns the total number of files found.
May throw an exception derived from |
|
Performs a search and replace operation: searches through the
string in for all occurrences of the current
expression, for each occurrence replaces the match with the format
string fmt. Uses flags
to determine what gets matched, and how the format string should
be treated. If copy is true then all unmatched
sections of input are copied unchanged to output, if the flag
format_first_only is set then only the first
occurance of the pattern found is replaced. Returns the new string.
See also format string syntax,
and |
|
Performs a search and replace operation: searches through the
string in for all occurrences of the current
expression, for each occurrence replaces the match with the format
string fmt. Uses flags
to determine what gets matched, and how the format string should
be treated. If copy is true then all unmatched
sections of input are copied unchanged to output, if the flag
format_first_only is set then only the first
occurance of the pattern found is replaced. Returns the new string.
See also format string syntax,
and |
|
Splits the input string and pushes each one onto the vector. If the expression contains no marked sub-expressions, then one string is outputted for each section of the input that does not match the expression. If the expression does contain marked sub-expressions, then outputs one string for each marked sub-expression each time a match occurs. Outputs no more than max_count strings. Before returning, deletes from the input string s all of the input that has been processed (all of the string if max_count was not reached). Returns the number of strings pushed onto the vector. |
|
Returns the position of what matched sub-expression i.
If |
|
Returns the length of what matched sub-expression i. If |
|
Returns true if sub-expression i was matched, false otherwise. |
|
Returns the line on which the match occurred, indexes start from
1 not zero, if no match occurred then returns |
|
Returns the number of marked sub-expressions contained in the expression. Note that this includes the whole match (sub-expression zero), so the value returned is always >= 1. |
|
Returns a copy of what matched sub-expression i.
If |
|
Returns |