Pattern matching with regular expressions. More...
#include <vul_reg_exp.h>
Public Member Functions | |
vul_reg_exp () | |
Creates an empty regular expression. | |
vul_reg_exp (char const *s) | |
Creates a regular expression from string s, and compiles s. | |
vul_reg_exp (vul_reg_exp const &) | |
Copy constructor. | |
~vul_reg_exp () | |
Frees space allocated for regular expression. | |
void | compile (char const *) |
Compiles char* --> regexp. | |
bool | find (char const *) |
true if regexp in char* arg. | |
bool | find (vcl_string const &) |
true if regexp in char* arg. | |
vcl_ptrdiff_t | start () const |
Returns the start index of the last item found. | |
vcl_ptrdiff_t | end () const |
Returns the end index of the last item found. | |
bool | operator== (vul_reg_exp const &) const |
Equality operator. | |
bool | operator!= (vul_reg_exp const &r) const |
Inequality operator. | |
bool | deep_equal (vul_reg_exp const &) const |
Same regexp and state?. | |
bool | is_valid () const |
Returns true if a valid RE is compiled and ready for pattern matching. | |
void | set_invalid () |
Invalidates regular expression. | |
vcl_ptrdiff_t | start (long n) const |
Return start index of nth submatch. | |
vcl_ptrdiff_t | end (long n) const |
Return end index of nth submatch. | |
vcl_string | match (int n) const |
Return nth submatch as a string. | |
Static Public Member Functions | |
static const char * | protect (char c) |
Return an expression that will match precisely c. | |
Private Member Functions | |
void | clear_bufs () |
private function to clear startp[] and endp[]. | |
Private Attributes | |
const char * | startp [vul_reg_exp_nsubexp] |
anchor point of start position for n-th matching regular expression. | |
const char * | endp [vul_reg_exp_nsubexp] |
anchor point of end position for n-th matching regular expression. | |
char | regstart |
Internal use only. | |
char | reganch |
Internal use only. | |
const char * | regmust |
Internal use only. | |
int | regmlen |
Internal use only. | |
char * | program |
int | progsize |
const char * | searchstring |
Pattern matching with regular expressions.
A regular expression allows a programmer to specify complex patterns that can be searched for and matched against the character string of a string object. In its simplest form, a regular expression is a sequence of characters used to search for exact character matches. However, many times the exact sequence to be found is not known, or only a match at the beginning or end of a string is desired. This regular expression class implements regular expression pattern matching as is found and implemented in many UNIX commands and utilities.
Example: The perl code
$filename =~ m"([a-z]+)\.cc";
print $1;
is written as follows in C++
vul_reg_exp re("([a-z]+)\\.cc"); re.find(filename); vcl_cout << re.match(1);
The regular expression class provides a convenient mechanism for specifying and manipulating regular expressions. The regular expression object allows specification of such patterns by using the following regular expression metacharacters:
Note that more than one of these metacharacters can be used in a single regular expression in order to create complex search patterns. For example, the pattern [^ab1-9] says to match any character sequence that does not begin with the characters "a", "b", or the characters "1" through "9".
Definition at line 79 of file vul_reg_exp.h.
vul_reg_exp::vul_reg_exp | ( | ) | [inline] |
Creates an empty regular expression.
Definition at line 98 of file vul_reg_exp.h.
vul_reg_exp::vul_reg_exp | ( | char const * | s | ) | [inline] |
Creates a regular expression from string s, and compiles s.
Definition at line 100 of file vul_reg_exp.h.
vul_reg_exp::vul_reg_exp | ( | vul_reg_exp const & | rxp | ) |
Copy constructor.
Copies the given regular expression.
Definition at line 126 of file vul_reg_exp.cxx.
vul_reg_exp::~vul_reg_exp | ( | ) | [inline] |
Frees space allocated for regular expression.
Definition at line 104 of file vul_reg_exp.h.
void vul_reg_exp::clear_bufs | ( | ) | [inline, private] |
private function to clear startp[] and endp[].
Definition at line 144 of file vul_reg_exp.h.
void vul_reg_exp::compile | ( | char const * | exp | ) |
Compiles char* --> regexp.
Compile a regular expression into internal code for later pattern matching.
Definition at line 393 of file vul_reg_exp.cxx.
bool vul_reg_exp::deep_equal | ( | vul_reg_exp const & | rxp | ) | const |
Same regexp and state?.
Returns true if have the same compiled regular expressions and the same start and end pointers.
Definition at line 169 of file vul_reg_exp.cxx.
vcl_ptrdiff_t vul_reg_exp::end | ( | ) | const [inline] |
Returns the end index of the last item found.
Definition at line 114 of file vul_reg_exp.h.
vcl_ptrdiff_t vul_reg_exp::end | ( | long | n | ) | const [inline] |
Return end index of nth submatch.
end(0) is the end of the full match.
Definition at line 131 of file vul_reg_exp.h.
bool vul_reg_exp::find | ( | char const * | string | ) |
true if regexp in char* arg.
Matches the regular expression to the given string.
Returns true if found, and sets start and end indexes accordingly.
Definition at line 949 of file vul_reg_exp.cxx.
bool vul_reg_exp::find | ( | vcl_string const & | s | ) |
true if regexp in char* arg.
Definition at line 940 of file vul_reg_exp.cxx.
bool vul_reg_exp::is_valid | ( | ) | const [inline] |
Returns true if a valid RE is compiled and ready for pattern matching.
Definition at line 122 of file vul_reg_exp.h.
vcl_string vul_reg_exp::match | ( | int | n | ) | const [inline] |
Return nth submatch as a string.
Definition at line 133 of file vul_reg_exp.h.
bool vul_reg_exp::operator!= | ( | vul_reg_exp const & | r | ) | const [inline] |
Inequality operator.
Definition at line 118 of file vul_reg_exp.h.
bool vul_reg_exp::operator== | ( | vul_reg_exp const & | rxp | ) | const |
Equality operator.
Returns true if two regular expressions have the same compiled program for pattern matching.
Definition at line 153 of file vul_reg_exp.cxx.
const char * vul_reg_exp::protect | ( | char | c | ) | [static] |
Return an expression that will match precisely c.
The returned string is owned by the function, and will be overwritten in subsequent calls.
This should be in thread local storage.
Definition at line 324 of file vul_reg_exp.cxx.
void vul_reg_exp::set_invalid | ( | ) | [inline] |
Invalidates regular expression.
Definition at line 124 of file vul_reg_exp.h.
vcl_ptrdiff_t vul_reg_exp::start | ( | ) | const [inline] |
Returns the start index of the last item found.
Definition at line 112 of file vul_reg_exp.h.
vcl_ptrdiff_t vul_reg_exp::start | ( | long | n | ) | const [inline] |
Return start index of nth submatch.
start(0) is the start of the full match.
Definition at line 128 of file vul_reg_exp.h.
const char* vul_reg_exp::endp[vul_reg_exp_nsubexp] [private] |
anchor point of end position for n-th matching regular expression.
Definition at line 84 of file vul_reg_exp.h.
char* vul_reg_exp::program [private] |
Definition at line 93 of file vul_reg_exp.h.
int vul_reg_exp::progsize [private] |
Definition at line 94 of file vul_reg_exp.h.
char vul_reg_exp::reganch [private] |
Internal use only.
Definition at line 88 of file vul_reg_exp.h.
int vul_reg_exp::regmlen [private] |
Internal use only.
Definition at line 92 of file vul_reg_exp.h.
const char* vul_reg_exp::regmust [private] |
Internal use only.
Definition at line 90 of file vul_reg_exp.h.
char vul_reg_exp::regstart [private] |
Internal use only.
Definition at line 86 of file vul_reg_exp.h.
const char* vul_reg_exp::searchstring [private] |
Definition at line 95 of file vul_reg_exp.h.
const char* vul_reg_exp::startp[vul_reg_exp_nsubexp] [private] |
anchor point of start position for n-th matching regular expression.
Definition at line 82 of file vul_reg_exp.h.