1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
28 Wrapper around POSIX extended regular expressions.
31 The manpage regex(7) for more information about POSIX regular expressions.
32 These differ somewhat from \c Perl and \c sed regular expressions.
37 \*---------------------------------------------------------------------------*/
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 // Forward declaration of classes
52 template<class T> class List;
54 /*---------------------------------------------------------------------------*\
55 Class regExp Declaration
56 \*---------------------------------------------------------------------------*/
62 //- Precompiled regular expression
63 mutable regex_t* preg_;
66 // Private Member Functions
68 //- Disallow default bitwise copy construct
69 regExp(const regExp&);
71 //- Disallow default bitwise assignment
72 void operator=(const regExp&);
76 // Static Member Functions
78 //- Is character a regular expression meta-character?
79 // any character: '.' \n
80 // quantifiers: '*', '+', '?' \n
81 // grouping: '(', '|', ')' \n
84 // Don't bother checking for '{digit}' bounds
85 inline static bool meta(char c)
89 (c == '.') // any character
90 || (c == '*' || c == '+' || c == '?') // quantifiers
91 || (c == '(' || c == ')' || c == '|') // grouping/branching
92 || (c == '[' || c == ']') // range
102 //- Construct from character array, optionally ignoring case
103 regExp(const char*, const bool ignoreCase=false);
105 //- Construct from std::string (or string), optionally ignoring case
106 regExp(const std::string&, const bool ignoreCase=false);
117 //- Return true if a precompiled expression does not exist
118 inline bool empty() const
123 //- Does a precompiled expression exist?
124 inline bool exists() const
126 return preg_ ? true : false;
129 //- Return the number of (groups)
130 inline int ngroups() const
132 return preg_ ? preg_->re_nsub : 0;
138 //- Compile pattern into a regular expression, optionally ignoring
140 void set(const char*, const bool ignoreCase=false) const;
142 //- Compile pattern into a regular expression, optionally ignoring
144 void set(const std::string&, const bool ignoreCase=false) const;
147 //- Release precompiled expression.
148 // Returns true if precompiled expression existed before clear
154 //- Find position within string.
155 // Returns the index where it begins or string::npos if not found
156 std::string::size_type find(const std::string& str) const;
158 //- Return true if it matches the entire string
159 // The begin-of-line (^) and end-of-line ($) anchors are implicit
160 bool match(const std::string&) const;
162 //- Return true if it matches and sets the sub-groups matched
163 // The begin-of-line (^) and end-of-line ($) anchors are implicit
164 bool match(const string&, List<string>& groups) const;
166 //- Return true if the regex was found within string
167 bool search(const std::string& str) const
169 return std::string::npos != find(str);
175 //- Assign and compile pattern from a character array
176 // Always case sensitive
177 void operator=(const char*);
179 //- Assign and compile pattern from string
180 // Always case sensitive
181 void operator=(const std::string&);
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 } // End namespace Foam
189 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 // ************************************************************************* //