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:
parent
2717aa5c7d
commit
461ac4b4cc
@ -62,18 +62,18 @@ int main(int argc, char *argv[])
|
||||
Info<< "true";
|
||||
if (re.ngroups())
|
||||
{
|
||||
Info<< groups;
|
||||
Info<< " groups:" << groups;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "false";
|
||||
if (re.match(str, true))
|
||||
if (re.search(str))
|
||||
{
|
||||
Info<< " partial match";
|
||||
}
|
||||
}
|
||||
Info << endl;
|
||||
Info<< endl;
|
||||
}
|
||||
|
||||
Info<<"test regExp(const char*) ..." << endl;
|
||||
|
@ -15,6 +15,7 @@
|
||||
( "a.*" "abcd" )
|
||||
( "a.*" "def" )
|
||||
( "d(.*)f" "def" )
|
||||
( " *([A-Za-z]+) *= *([^ /]+) *(//.*)?" " keyword = value // settings" )
|
||||
)
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -44,8 +44,7 @@ SourceFiles
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
//- Return the indices of the strings in the list
|
||||
// that match the given regular expression
|
||||
//- Return list indices for the strings matching the regular expression
|
||||
// partial matches are optional
|
||||
template<class StringType>
|
||||
labelList findStrings
|
||||
|
@ -48,7 +48,7 @@ labelList findStrings
|
||||
label matchI = 0;
|
||||
forAll(lst, elemI)
|
||||
{
|
||||
if (re.match(lst[elemI], partialMatch))
|
||||
if (partialMatch ? re.search(lst[elemI]) : re.match(lst[elemI]))
|
||||
{
|
||||
matched[matchI++] = elemI;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user