regExp.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2017 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  //- Disallow default bitwise copy construct
73  regExp(const regExp&);
74 
75  //- Disallow default bitwise assignment
76  void operator=(const regExp&);
77 
78  //- Return true if it matches and sets the sub-groups matched.
79  // Templated to support both std::string and Foam::string
80  template<class StringType>
81  bool matchGrouping
82  (
83  const std::string&,
84  List<StringType>& groups
85  ) const;
86 
87 
88 public:
89 
90  // Static Member Functions
91 
92  //- Is character a regular expression meta-character?
93  // any character: '.' \n
94  // quantifiers: '*', '+', '?' \n
95  // grouping: '(', '|', ')' \n
96  // range: '[', ']' \n
97  //
98  // Don't bother checking for '{digit}' bounds
99  inline static bool meta(char c)
100  {
101  return
102  (
103  (c == '.') // any character
104  || (c == '*' || c == '+' || c == '?') // quantifiers
105  || (c == '(' || c == ')' || c == '|') // grouping/branching
106  || (c == '[' || c == ']') // range
107  );
108  }
109 
110 
111  // Constructors
112 
113  //- Construct null
114  regExp();
115 
116  //- Construct from character array, optionally ignoring case
117  regExp(const char*, const bool ignoreCase=false);
118 
119  //- Construct from std::string (or string), optionally ignoring case
120  regExp(const std::string&, const bool ignoreCase=false);
121 
122 
123  //- Destructor
124  ~regExp();
125 
126 
127  // Member functions
128 
129  // Access
130 
131  //- Return true if a precompiled expression does not exist
132  inline bool empty() const
133  {
134  return !preg_;
135  }
136 
137  //- Does a precompiled expression exist?
138  inline bool exists() const
139  {
140  return preg_ ? true : false;
141  }
142 
143  //- Return the number of (groups)
144  inline int ngroups() const
145  {
146  return int(preg_ ? preg_->re_nsub : 0);
147  }
148 
149 
150  // Editing
151 
152  //- Compile pattern into a regular expression,
153  // optionally ignoring case
154  void set(const char*, const bool ignoreCase=false) const;
155 
156  //- Compile pattern into a regular expression,
157  // optionally ignoring case
158  void set(const std::string&, const bool ignoreCase=false) const;
159 
160  //- Release precompiled expression.
161  // Returns true if precompiled expression existed before clear
162  bool clear() const;
163 
164 
165  // Searching
166 
167  //- Find position within string.
168  // Returns the index where it begins or string::npos if not found
169  std::string::size_type find(const std::string& str) const;
170 
171  //- Return true if it matches the entire string
172  // The begin-of-line (^) and end-of-line ($) anchors are implicit
173  bool match(const std::string&) const;
174 
175  //- Return true if it matches and sets the sub-groups matched
176  // The begin-of-line (^) and end-of-line ($) anchors are implicit
177  bool match(const std::string&, List<std::string>& groups) const;
178 
179  //- Return true if it matches and sets the sub-groups matched
180  // The begin-of-line (^) and end-of-line ($) anchors are implicit
181  bool match(const std::string&, List<string>& groups) const;
182 
183  //- Return true if the regex was found within string
184  bool search(const std::string& str) const
185  {
186  return std::string::npos != find(str);
187  }
188 
189 
190  // Member Operators
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
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:60
static bool meta(char c)
Is character a regular expression meta-character?
Definition: regExp.H:98
~regExp()
Destructor.
Definition: regExp.C:108
bool exists() const
Does a precompiled expression exist?
Definition: regExp.H:137
bool search(const std::string &str) const
Return true if the regex was found within string.
Definition: regExp.H:183
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
int ngroups() const
Return the number of (groups)
Definition: regExp.H:143
std::string::size_type find(const std::string &str) const
Find position within string.
Definition: regExp.C:184
const dimensionedScalar c
Speed of light in a vacuum.
regExp()
Construct null.
Definition: regExp.C:84
bool empty() const
Return true if a precompiled expression does not exist.
Definition: regExp.H:131
Namespace for OpenFOAM.