1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. 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_;
65 // Private member functions
67 //- Disallow default bitwise copy construct
68 regExp(const regExp&);
70 //- Disallow default bitwise assignment
71 void operator=(const regExp&);
75 //- Is character a regular expression meta-character?
76 // any character: '.' \n
77 // quantifiers: '*', '+', '?' \n
78 // grouping: '(', '|', ')' \n
81 // Don't bother checking for '{digit}' bounds
82 inline static bool meta(char c)
86 (c == '.') // any character
87 || (c == '*' || c == '+' || c == '?') // quantifiers
88 || (c == '(' || c == ')' || c == '|') // grouping/branching
89 || (c == '[' || c == ']') // range
99 //- Construct from character array, optionally ignoring case
100 regExp(const char*, const bool ignoreCase=false);
102 //- Construct from std::string (or string), optionally ignoring case
103 regExp(const std::string&, const bool ignoreCase=false);
114 //- Return true if a precompiled expression does not exist
115 inline bool empty() const
120 //- Does a precompiled expression exist?
121 inline bool exists() const
123 return preg_ ? true : false;
126 //- Return the number of (groups)
127 inline int ngroups() const
129 return preg_ ? preg_->re_nsub : 0;
135 //- Compile pattern into a regular expression, optionally ignoring case
136 void set(const char*, const bool ignoreCase=false) const;
138 //- Compile pattern into a regular expression, optionally ignoring case
139 void set(const std::string&, const bool ignoreCase=false) const;
142 //- Release precompiled expression.
143 // Returns true if precompiled expression existed before clear
149 //- Find position within string.
150 // Returns the index where it begins or string::npos if not found
151 std::string::size_type find(const std::string& str) const;
153 //- Return true if it matches the entire string
154 // The begin-of-line (^) and end-of-line ($) anchors are implicit
155 bool match(const std::string&) const;
157 //- Return true if it matches and sets the sub-groups matched
158 // The begin-of-line (^) and end-of-line ($) anchors are implicit
159 bool match(const string&, List<string>& groups) const;
161 //- Return true if the regex was found in within string
162 bool search(const std::string& str) const
164 return std::string::npos != find(str);
170 //- Assign and compile pattern from a character array
171 // Always case sensitive
172 void operator=(const char*);
174 //- Assign and compile pattern from string
175 // Always case sensitive
176 void operator=(const std::string&);
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
183 } // End namespace Foam
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
189 // ************************************************************************* //