1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include <sys/types.h>
33 #include "IOstreams.H"
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 Foam::regExp::regExp()
43 Foam::regExp::regExp(const char* pattern, const bool ignoreCase)
47 set(pattern, ignoreCase);
51 Foam::regExp::regExp(const std::string& pattern, const bool ignoreCase)
55 set(pattern.c_str(), ignoreCase);
59 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
61 Foam::regExp::~regExp()
67 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
69 void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
73 // avoid NULL pointer and zero-length patterns
74 if (pattern && *pattern)
78 int cflags = REG_EXTENDED;
84 if (regcomp(preg_, pattern, cflags) != 0)
88 "regExp::set(const char*)"
89 ) << "Failed to compile regular expression '" << pattern << "'"
96 void Foam::regExp::set(const std::string& pattern, const bool ignoreCase) const
98 return set(pattern.c_str(), ignoreCase);
102 bool Foam::regExp::clear() const
117 std::string::size_type Foam::regExp::find(const std::string& str) const
119 if (preg_ && str.size())
122 regmatch_t pmatch[1];
124 if (regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0)
126 return pmatch[0].rm_so;
134 bool Foam::regExp::match(const std::string& str) const
136 if (preg_ && str.size())
139 regmatch_t pmatch[1];
141 // also verify that the entire string was matched
142 // pmatch[0] is the entire match
145 regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
146 && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
157 bool Foam::regExp::match(const string& str, List<string>& groups) const
159 if (preg_ && str.size())
161 size_t nmatch = ngroups() + 1;
162 regmatch_t pmatch[nmatch];
164 // also verify that the entire string was matched
165 // pmatch[0] is the entire match
166 // pmatch[1..] are the (...) sub-groups
169 regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
170 && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
173 groups.setSize(ngroups());
176 for (size_t matchI = 1; matchI < nmatch; matchI++)
178 if (pmatch[matchI].rm_so != -1 && pmatch[matchI].rm_eo != -1)
180 groups[groupI] = str.substr
182 pmatch[matchI].rm_so,
183 pmatch[matchI].rm_eo - pmatch[matchI].rm_so
188 groups[groupI].clear();
202 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
204 void Foam::regExp::operator=(const char* pat)
210 void Foam::regExp::operator=(const std::string& pat)
216 // ************************************************************************* //