All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
regExp.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 Class
25  Foam::regExp
26 
27 Description
28  Wrapper around POSIX extended regular expressions.
29 
30  The PCRE '(?i)' extension is provided to compile the regular expression
31  as being case-insensitive.
32 
33 See also
34  The manpage regex(7) for more information about POSIX regular expressions.
35  These differ somewhat from \c Perl and \c sed regular expressions.
36 
37 SourceFiles
38  regExp.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef regExp_H
43 #define regExp_H
44 
45 #include <regex.h>
46 #include <string>
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward declaration of classes
54 class string;
55 template<class T> class List;
56 
57 
58 /*---------------------------------------------------------------------------*\
59  Class regExp Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class regExp
63 {
64  // Private Data
65 
66  //- Precompiled regular expression
67  mutable regex_t* preg_;
68 
69 
70  // Private Member Functions
71 
72  //- Return true if it matches and sets the sub-groups matched.
73  // Templated to support both std::string and Foam::string
74  template<class StringType>
75  bool matchGrouping
76  (
77  const std::string&,
78  List<StringType>& groups
79  ) const;
80 
81 
82 public:
83 
84  // Static Member Functions
85 
86  //- Is character a regular expression meta-character?
87  // any character: '.' \n
88  // quantifiers: '*', '+', '?' \n
89  // grouping: '(', '|', ')' \n
90  // range: '[', ']' \n
91  //
92  // Don't bother checking for '{digit}' bounds
93  inline static bool meta(char c)
94  {
95  return
96  (
97  (c == '.') // any character
98  || (c == '*' || c == '+' || c == '?') // quantifiers
99  || (c == '(' || c == ')' || c == '|') // grouping/branching
100  || (c == '[' || c == ']') // range
101  );
102  }
103 
104 
105  // Constructors
106 
107  //- Construct null
108  regExp();
109 
110  //- Construct from character array, optionally ignoring case
111  regExp(const char*, const bool ignoreCase=false);
112 
113  //- Construct from std::string (or string), optionally ignoring case
114  regExp(const std::string&, const bool ignoreCase=false);
115 
116  //- Disallow default bitwise copy construction
117  regExp(const regExp&) = delete;
118 
119 
120  //- Destructor
121  ~regExp();
122 
123 
124  // Member Functions
125 
126  // Access
127 
128  //- Return true if a precompiled expression does not exist
129  inline bool empty() const
130  {
131  return !preg_;
132  }
133 
134  //- Does a precompiled expression exist?
135  inline bool exists() const
136  {
137  return preg_ ? true : false;
138  }
139 
140  //- Return the number of (groups)
141  inline int ngroups() const
142  {
143  return int(preg_ ? preg_->re_nsub : 0);
144  }
145 
146 
147  // Editing
148 
149  //- Compile pattern into a regular expression,
150  // optionally ignoring case
151  void set(const char*, const bool ignoreCase=false) const;
152 
153  //- Compile pattern into a regular expression,
154  // optionally ignoring case
155  void set(const std::string&, const bool ignoreCase=false) const;
156 
157  //- Release precompiled expression.
158  // Returns true if precompiled expression existed before clear
159  bool clear() const;
160 
161 
162  // Searching
163 
164  //- Find position within string.
165  // Returns the index where it begins or string::npos if not found
166  std::string::size_type find(const std::string& str) const;
167 
168  //- Return true if it matches the entire string
169  // The begin-of-line (^) and end-of-line ($) anchors are implicit
170  bool match(const std::string&) const;
171 
172  //- Return true if it matches and sets the sub-groups matched
173  // The begin-of-line (^) and end-of-line ($) anchors are implicit
174  bool match(const std::string&, List<std::string>& groups) const;
175 
176  //- Return true if it matches and sets the sub-groups matched
177  // The begin-of-line (^) and end-of-line ($) anchors are implicit
178  bool match(const std::string&, List<string>& groups) const;
179 
180  //- Return true if the regex was found within string
181  bool search(const std::string& str) const
182  {
183  return std::string::npos != find(str);
184  }
185 
186 
187  // Member Operators
188 
189  //- Disallow default bitwise assignment
190  void operator=(const regExp&) = delete;
191 
192  //- Assign and compile pattern from a character array
193  // Always case sensitive
194  void operator=(const char*);
195 
196  //- Assign and compile pattern from string
197  // Always case sensitive
198  void operator=(const std::string&);
199 };
200 
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 } // End namespace Foam
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 #endif
209 
210 // ************************************************************************* //
bool clear() const
Release precompiled expression.
Definition: regExp.C:169
void operator=(const regExp &)=delete
Disallow default bitwise assignment.
bool match(const std::string &) const
Return true if it matches the entire string.
Definition: regExp.C:201
Wrapper around POSIX extended regular expressions.
Definition: regExp.H:61
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
static bool meta(char c)
Is character a regular expression meta-character?
Definition: regExp.H:92
~regExp()
Destructor.
Definition: regExp.C:108
const dimensionedScalar & c
Speed of light in a vacuum.
bool exists() const
Does a precompiled expression exist?
Definition: regExp.H:134
bool search(const std::string &str) const
Return true if the regex was found within string.
Definition: regExp.H:180
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
int ngroups() const
Return the number of (groups)
Definition: regExp.H:140
std::string::size_type find(const std::string &str) const
Find position within string.
Definition: regExp.C:184
regExp()
Construct null.
Definition: regExp.C:84
bool empty() const
Return true if a precompiled expression does not exist.
Definition: regExp.H:128
Namespace for OpenFOAM.