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
29 Wrapper around POSIX extended regular expressions.
32 The manpage regex(7) for more information about POSIX regular expressions.
33 These differ somewhat from @c Perl and @c sed regular expressions.
38 \*---------------------------------------------------------------------------*/
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 // Forward declaration of classes
53 template<class T> class List;
55 /*---------------------------------------------------------------------------*\
56 Class regExp Declaration
57 \*---------------------------------------------------------------------------*/
63 //- Precompiled regular expression
64 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 //- Is character a regular expression meta-character?
77 // any character: '.' \n
78 // quantifiers: '*', '+', '?' \n
79 // grouping: '(', '|', ')' \n
82 // Don't bother checking for '{digit}' bounds
83 inline static bool meta(char c)
87 (c == '.') // any character
88 || (c == '*' || c == '+' || c == '?') // quantifiers
89 || (c == '(' || c == ')' || c == '|') // grouping/branching
90 || (c == '[' || c == ']') // range
100 //- Construct from character array, optionally ignoring case
101 regExp(const char*, const bool ignoreCase=false);
103 //- Construct from std::string (or string), optionally ignoring case
104 regExp(const std::string&, const bool ignoreCase=false);
115 //- Return true if a precompiled expression does not exist
116 inline bool empty() const
121 //- Does a precompiled expression exist?
122 inline bool exists() const
124 return preg_ ? true : false;
127 //- Return the number of (groups)
128 inline int ngroups() const
130 return preg_ ? preg_->re_nsub : 0;
136 //- Compile pattern into a regular expression, optionally ignoring case
137 void set(const char*, const bool ignoreCase=false) const;
139 //- Compile pattern into a regular expression, optionally ignoring case
140 void set(const std::string&, const bool ignoreCase=false) const;
143 //- Release precompiled expression.
144 // Returns true if precompiled expression existed before clear
150 //- Find position within string.
151 // Returns the index where it begins or string::npos if not found
152 std::string::size_type find(const std::string& str) const;
154 //- Return true if it matches the entire string
155 // The begin-of-line (^) and end-of-line ($) anchors are implicit
156 bool match(const std::string&) const;
158 //- Return true if it matches and sets the sub-groups matched
159 // The begin-of-line (^) and end-of-line ($) anchors are implicit
160 bool match(const string&, List<string>& groups) const;
162 //- Return true if the regex was found in within string
163 bool search(const std::string& str) const
165 return std::string::npos != find(str);
171 //- Assign and compile pattern from a character array
172 // Always case sensitive
173 void operator=(const char*);
175 //- Assign and compile pattern from string
176 // Always case sensitive
177 void operator=(const std::string&);
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 } // End namespace Foam
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
190 // ************************************************************************* //