From 461ac4b4ccff3a68fcab00085c10c0ea8eddda0f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 5 Jan 2009 09:37:52 +0100 Subject: [PATCH] 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 --- applications/test/regex/regexTest.C | 6 ++-- applications/test/regex/testRegexps | 1 + src/OSspecific/Unix/regExp.C | 29 ++++++++++++++----- src/OSspecific/Unix/regExp.H | 21 ++++++++++---- src/OpenFOAM/primitives/Lists/stringListOps.H | 3 +- .../primitives/Lists/stringListOpsTemplates.C | 2 +- 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/applications/test/regex/regexTest.C b/applications/test/regex/regexTest.C index 49b6475f80..040d7bb397 100644 --- a/applications/test/regex/regexTest.C +++ b/applications/test/regex/regexTest.C @@ -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; diff --git a/applications/test/regex/testRegexps b/applications/test/regex/testRegexps index eeae1d865a..72287eea43 100644 --- a/applications/test/regex/testRegexps +++ b/applications/test/regex/testRegexps @@ -15,6 +15,7 @@ ( "a.*" "abcd" ) ( "a.*" "def" ) ( "d(.*)f" "def" ) + ( " *([A-Za-z]+) *= *([^ /]+) *(//.*)?" " keyword = value // settings" ) ) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OSspecific/Unix/regExp.C b/src/OSspecific/Unix/regExp.C index b2a0ddd5a7..e6481bd1f7 100644 --- a/src/OSspecific/Unix/regExp.C +++ b/src/OSspecific/Unix/regExp.C @@ -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& 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 diff --git a/src/OSspecific/Unix/regExp.H b/src/OSspecific/Unix/regExp.H index 553decd7fd..1cf9601c76 100644 --- a/src/OSspecific/Unix/regExp.H +++ b/src/OSspecific/Unix/regExp.H @@ -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 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& 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 diff --git a/src/OpenFOAM/primitives/Lists/stringListOps.H b/src/OpenFOAM/primitives/Lists/stringListOps.H index 3b2ae883f3..1da5f272b9 100644 --- a/src/OpenFOAM/primitives/Lists/stringListOps.H +++ b/src/OpenFOAM/primitives/Lists/stringListOps.H @@ -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 labelList findStrings diff --git a/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C b/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C index 4accbd0657..f0ed36493d 100644 --- a/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C +++ b/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C @@ -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; }