This file defines a C++ wrapper around the standard regular expression functionality. More...
#include <simple_regex.h>
Classes | |
struct | match |
match a struct describing a sub-string that matched the pattern. More... | |
Public Member Functions | |
bool | anchored () const |
returns true if the regular expression is anchored to the beginning of the string | |
bool | empty () const |
returns true if the expression is an empty string | |
char const * | error () const |
error() text of error message | |
match const * | matches () const |
matches() array of matches found | |
bool | ok () const |
ok() regex was valid? | |
operator std::string const & () const | |
what was the regex anyway? | |
int | operator() (std::string const &s, int offset=0, int length=0x7ffffff) const |
Check a string or character buffer for a match and return the number of matches. See matches() below. | |
int | operator() (char const *string, int stringLength) const |
Check a string for a match and setup internal variables to refer to the parts of the string that matched. See matches(). | |
SimpleRegex & | operator= (char const *r) |
operator= copy from char * | |
SimpleRegex & | operator= (std::string const &r) |
operator= copy from std::string | |
SimpleRegex & | operator= (SimpleRegex const &r) |
operator= copy from another SimpleRegex | |
std::string const & | options () const |
bool | replace (std::string &rv, std::string const &source, std::string const &replacement, int maxIterations=0x7fffffff) const |
Replace a pattern defined by this SimpleRegex within a source string with a replacement at most maxReplacements times and return true if at least one replacement occurred. | |
void | reset (std::string const &expression, std::string const &options) |
SimpleRegex (SimpleRegex const &r) | |
SimpleRegex() Copy construct. | |
SimpleRegex (char const *expression, char const *options="") | |
Construct from a char *. | |
SimpleRegex (std::string const &expression="", std::string const &options="") | |
Construct a regular expression helper object from a string. | |
std::string | substitute (std::string const &original, std::string const &replacement) const |
This function replace the text in the original string which is identified by the match array of this regular expression with a value computed from the replacement string. Here's how you use the substitute method: | |
~SimpleRegex () | |
~SimpleRegex() destructor | |
Private Types | |
enum | constants_ { max_matches = 20 } |
Private Member Functions | |
std::string | expand (std::string const &, std::string const &) const |
used in the substitute function as a helper. | |
Private Attributes | |
regex_t | compiled_expression |
The real regex_t object that does < the work. | |
char const * | error_ |
construction error message if any | |
std::string | expression_ |
saved for later use -- in copying | |
int | match_count_ |
set when an operator function is called < set to 0 when constructed or copied! | |
match | matches_ [max_matches] |
buffer hold matches from a given search | |
bool | ok_ |
compilation was ok, not searching | |
std::string | options_ |
flags used in compiling |
This file defines a C++ wrapper around the standard regular expression functionality.
A simplistic interface to the regular expression. This is meant to only work with short string -- 200 characters or less. Construction equals compilation as does copy construction and assignment. To search a string, first construct a SimpleRegex then treat it like a function and call it. See the operator() methods for variants. The operators return the number of matches. Errors and matched items are mutable -- so just because one of these things is declared const, doesn't mean the error message and the matched items remain inviolate.
Definition at line 43 of file simple_regex.h.
enum constants_ [private] |
Definition at line 73 of file simple_regex.h.
SimpleRegex | ( | std::string const & | expression = "" , |
|
std::string const & | options = "" | |||
) |
Construct a regular expression helper object from a string.
expression | a std::string defining the regular expression to process. | |
options | the options to use when compiling the regular expression. The options string should either be empty, or be equal to "i" to indicate case insensitive comparisons. |
Definition at line 99 of file simple_regex.cxx.
SimpleRegex | ( | char const * | expression, | |
char const * | options = "" | |||
) |
Construct from a char *.
expression | a char const * defining the regular expression to be compiled. options a char const * defining the option to the compilation process. |
Construct a regular expression helper object from a string.
expression | a std::string defining the regular expression to process. | |
options | the options to use when compiling the regular expression. The options string should either be empty, or be equal to "i" to indicate case insensitive comparisons. |
Definition at line 127 of file simple_regex.cxx.
SimpleRegex | ( | SimpleRegex const & | r | ) |
SimpleRegex() Copy construct.
Definition at line 151 of file simple_regex.cxx.
~SimpleRegex | ( | ) |
~SimpleRegex() destructor
Definition at line 219 of file simple_regex.cxx.
bool anchored | ( | ) | const |
returns true if the regular expression is anchored to the beginning of the string
Definition at line 442 of file simple_regex.cxx.
bool empty | ( | ) | const |
returns true if the expression is an empty string
Definition at line 154 of file simple_regex.h.
char const* error | ( | ) | const |
error() text of error message
Definition at line 148 of file simple_regex.h.
string expand | ( | std::string const & | original, | |
std::string const & | in | |||
) | const [private] |
used in the substitute function as a helper.
Definition at line 339 of file simple_regex.cxx.
match const* matches | ( | ) | const |
matches() array of matches found
Definition at line 161 of file simple_regex.h.
bool ok | ( | ) | const |
ok() regex was valid?
Definition at line 144 of file simple_regex.h.
operator std::string const & | ( | ) | const |
what was the regex anyway?
Definition at line 157 of file simple_regex.h.
int operator() | ( | std::string const & | s, | |
int | offset = 0 , |
|||
int | length = 0x7ffffff | |||
) | const |
Check a string or character buffer for a match and return the number of matches. See matches() below.
Definition at line 277 of file simple_regex.cxx.
int operator() | ( | char const * | string, | |
int | stringLength | |||
) | const |
Check a string for a match and setup internal variables to refer to the parts of the string that matched. See matches().
Definition at line 226 of file simple_regex.cxx.
SimpleRegex & operator= | ( | char const * | r | ) |
operator= copy from char *
Definition at line 189 of file simple_regex.cxx.
SimpleRegex & operator= | ( | std::string const & | r | ) |
operator= copy from std::string
Definition at line 206 of file simple_regex.cxx.
SimpleRegex & operator= | ( | SimpleRegex const & | r | ) |
operator= copy from another SimpleRegex
Definition at line 173 of file simple_regex.cxx.
std::string const& options | ( | ) | const |
Definition at line 146 of file simple_regex.h.
bool replace | ( | std::string & | rv, | |
std::string const & | source, | |||
std::string const & | replacement, | |||
int | maxIterations = 0x7fffffff | |||
) | const |
Replace a pattern defined by this SimpleRegex within a source string with a replacement at most maxReplacements times and return true if at least one replacement occurred.
Definition at line 452 of file simple_regex.cxx.
void reset | ( | std::string const & | expression, | |
std::string const & | options | |||
) |
string substitute | ( | std::string const & | original, | |
std::string const & | replacement | |||
) | const |
This function replace the text in the original string which is identified by the match array of this regular expression with a value computed from the replacement string. Here's how you use the substitute method:
1. construct a regular expression which can be used to perform regex find.
2. execute this regular expression on some string value.
3. if a match was found, use the substitute method to compute a a new string which is a copy of the comparand string value with the matched parts replaced by a new string computed from 'replacement'.
The replacement string is used to compute a replacement value and contain literal text and also can contain \0 ... \9. When these substrings occur, they are replaced with text from the original comparand string. See the man page for ex, sed, perl, etc for regular expression substitution logic.
Definition at line 415 of file simple_regex.cxx.
regex_t compiled_expression [private] |
The real regex_t object that does < the work.
Definition at line 67 of file simple_regex.h.
char const* error_ [mutable, private] |
construction error message if any
Definition at line 80 of file simple_regex.h.
std::string expression_ [private] |
saved for later use -- in copying
Definition at line 70 of file simple_regex.h.
int match_count_ [mutable, private] |
set when an operator function is called < set to 0 when constructed or copied!
Definition at line 84 of file simple_regex.h.
buffer hold matches from a given search
Definition at line 78 of file simple_regex.h.
bool ok_ [private] |
compilation was ok, not searching
Definition at line 82 of file simple_regex.h.
std::string options_ [private] |
flags used in compiling
Definition at line 71 of file simple_regex.h.