mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -62,18 +62,18 @@ int main(int argc, char *argv[])
|
|||||||
Info<< "true";
|
Info<< "true";
|
||||||
if (re.ngroups())
|
if (re.ngroups())
|
||||||
{
|
{
|
||||||
Info<< groups;
|
Info<< " groups:" << groups;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Info<< "false";
|
Info<< "false";
|
||||||
if (re.match(str, true))
|
if (re.search(str))
|
||||||
{
|
{
|
||||||
Info<< " partial match";
|
Info<< " partial match";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Info << endl;
|
Info<< endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Info<<"test regExp(const char*) ..." << endl;
|
Info<<"test regExp(const char*) ..." << endl;
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
( "a.*" "abcd" )
|
( "a.*" "abcd" )
|
||||||
( "a.*" "def" )
|
( "a.*" "def" )
|
||||||
( "d(.*)f" "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())
|
if (preg_ && str.size())
|
||||||
{
|
{
|
||||||
size_t nmatch = 1;
|
size_t nmatch = 1;
|
||||||
regmatch_t pmatch[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
|
// pmatch[0] is the entire match
|
||||||
if
|
if
|
||||||
(
|
(
|
||||||
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
|
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
|
||||||
&&
|
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
|
||||||
(
|
|
||||||
partial
|
|
||||||
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@ -139,7 +152,7 @@ bool Foam::regExp::match(const string& str, List<string>& groups) const
|
|||||||
size_t nmatch = ngroups() + 1;
|
size_t nmatch = ngroups() + 1;
|
||||||
regmatch_t pmatch[nmatch];
|
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[0] is the entire match
|
||||||
// pmatch[1..] are the (...) sub-groups
|
// pmatch[1..] are the (...) sub-groups
|
||||||
if
|
if
|
||||||
|
|||||||
@ -28,9 +28,6 @@ Class
|
|||||||
Description
|
Description
|
||||||
Wrapper around POSIX extended regular expressions.
|
Wrapper around POSIX extended regular expressions.
|
||||||
|
|
||||||
The beginning-of-line (^) and the end-of-line ($) anchors are implicit
|
|
||||||
by default.
|
|
||||||
|
|
||||||
SeeAlso
|
SeeAlso
|
||||||
The manpage regex(7) for more information about POSIX regular expressions.
|
The manpage regex(7) for more information about POSIX regular expressions.
|
||||||
These differ somewhat from @c Perl and @c sed regular expressions.
|
These differ somewhat from @c Perl and @c sed regular expressions.
|
||||||
@ -132,12 +129,24 @@ public:
|
|||||||
// Returns true if precompiled expression existed before clear
|
// Returns true if precompiled expression existed before clear
|
||||||
bool clear() const;
|
bool clear() const;
|
||||||
|
|
||||||
//- Return true if it matches, partial matches are optional
|
//- Find position within string.
|
||||||
bool match(const std::string&, bool partial=false) const;
|
// 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
|
//- 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;
|
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
|
// Member Operators
|
||||||
|
|
||||||
|
|||||||
@ -44,8 +44,7 @@ SourceFiles
|
|||||||
namespace Foam
|
namespace Foam
|
||||||
{
|
{
|
||||||
|
|
||||||
//- Return the indices of the strings in the list
|
//- Return list indices for the strings matching the regular expression
|
||||||
// that match the given regular expression
|
|
||||||
// partial matches are optional
|
// partial matches are optional
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
labelList findStrings
|
labelList findStrings
|
||||||
|
|||||||
@ -48,7 +48,7 @@ labelList findStrings
|
|||||||
label matchI = 0;
|
label matchI = 0;
|
||||||
forAll(lst, elemI)
|
forAll(lst, elemI)
|
||||||
{
|
{
|
||||||
if (re.match(lst[elemI], partialMatch))
|
if (partialMatch ? re.search(lst[elemI]) : re.match(lst[elemI]))
|
||||||
{
|
{
|
||||||
matched[matchI++] = elemI;
|
matched[matchI++] = elemI;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user