All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
RegexPipe.h
Go to the documentation of this file.
1 // Copyright 2015 Thomas Trapp
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEXT_REGEX_PIPE_H_INCLUDED
16 #define HEXT_REGEX_PIPE_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::RegexPipe
20 
21 #include "hext/Cloneable.h"
22 #include "hext/StringPipe.h"
23 
24 #include <string>
25 
26 #include <boost/regex.hpp>
27 
28 
29 namespace hext {
30 
31 
32 /// Filters a string according to a given regex.
33 ///
34 /// @anchor RegexPipeBehavior
35 /// @par Regex behavior:
36 /// A regex containing a capture group will produce only the matched
37 /// content of that capture group, otherwise the whole regex match is
38 /// returned. All capture groups after the first one will be ignored.
39 ///
40 /// @par Example regex:
41 /// Input | Regex | Result
42 /// ----------------------|-------------------|-----------
43 /// Highway 61 revisited | `\d+` | 61
44 /// Highway 61 revisited | `Highway \d+` | Highway 61
45 /// Highway 61 revisited | `Highway (\d+)` | 61
46 /// Highway 61 revisited | `\w+` | Highway
47 /// Highway 61 revisited | `(\w+) (\d+)` | Highway (not an error)
48 class RegexPipe final : public hext::Cloneable<RegexPipe, StringPipe>
49 {
50 public:
51  /// Constructs a RegexPipe from a boost::regex.
52  ///
53  /// @param regex: A regular expression that is applied to the string.
54  /// See @ref RegexPipeBehavior.
55  explicit RegexPipe(boost::regex regex);
56 
57  /// Filters the string according to the regex given in the constructor.
58  /// See @ref RegexPipeBehavior.
59  std::string transform(std::string str) const override;
60 
61 private:
62  boost::regex regex_;
63 };
64 
65 
66 } // namespace hext
67 
68 
69 #endif // HEXT_REGEX_PIPE_H_INCLUDED
70 
Declares hext::StringPipe.
std::string transform(std::string str) const override
Filters the string according to the regex given in the constructor.
Filters a string according to a given regex.
Definition: RegexPipe.h:48
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
Defines template hext::Cloneable.
RegexPipe(boost::regex regex)
Constructs a RegexPipe from a boost::regex.