regExp - separate full match from partial match, add find()

- match() only does a full match
  - find() and search() do partial matches
    search() is similar to the name coming into C++ TR1
This commit is contained in:
Mark Olesen
2009-01-05 09:37:52 +01:00
parent 2717aa5c7d
commit 461ac4b4cc
6 changed files with 42 additions and 20 deletions

View File

@ -105,23 +105,36 @@ bool Foam::regExp::clear() const
}
bool Foam::regExp::match(const std::string& str, bool partial) const
std::string::size_type Foam::regExp::find(const std::string& str) const
{
if (preg_ && str.size())
{
size_t nmatch = 1;
regmatch_t pmatch[1];
// match and also verify that the entire string was matched
if (regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0)
{
return pmatch[0].rm_so;
}
}
return string::npos;
}
bool Foam::regExp::match(const std::string& str) const
{
if (preg_ && str.size())
{
size_t nmatch = 1;
regmatch_t pmatch[1];
// also verify that the entire string was matched
// pmatch[0] is the entire match
if
(
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&&
(
partial
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
{
return true;
@ -139,7 +152,7 @@ bool Foam::regExp::match(const string& str, List<string>& groups) const
size_t nmatch = ngroups() + 1;
regmatch_t pmatch[nmatch];
// match and also verify that the entire string was matched
// also verify that the entire string was matched
// pmatch[0] is the entire match
// pmatch[1..] are the (...) sub-groups
if

View File

@ -28,9 +28,6 @@ Class
Description
Wrapper around POSIX extended regular expressions.
The beginning-of-line (^) and the end-of-line ($) anchors are implicit
by default.
SeeAlso
The manpage regex(7) for more information about POSIX regular expressions.
These differ somewhat from @c Perl and @c sed regular expressions.
@ -56,7 +53,7 @@ class string;
template<class T> class List;
/*---------------------------------------------------------------------------*\
Class regExp Declaration
Class regExp Declaration
\*---------------------------------------------------------------------------*/
class regExp
@ -132,12 +129,24 @@ public:
// Returns true if precompiled expression existed before clear
bool clear() const;
//- Return true if it matches, partial matches are optional
bool match(const std::string&, bool partial=false) const;
//- Find position within string.
// Returns the index where it begins or string::npos if not found
std::string::size_type find(const std::string& str) const;
//- Return true if it matches the entire string
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string&) const;
//- Return true if it matches and sets the sub-groups matched
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const string&, List<string>& groups) const;
//- Return true if the regex was found in within string
bool search(const std::string& str) const
{
return std::string::npos != find(str);
}
// Member Operators