libhext: C++ Library Documentation  1.0.8-3ad0ae4
RegexPipe.h
Go to the documentation of this file.
1 // Copyright 2015, 2016 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 #include "hext/Visibility.h"
24 
25 #include <string>
26 
27 #include <boost/regex.hpp>
28 
29 
30 namespace hext {
31 
32 
33 /// Filters a string according to a given regex.
34 ///
35 /// @anchor RegexPipeBehavior
36 /// @par Regex behavior:
37 /// A regex containing a capture group will produce only the matched
38 /// content of that capture group, otherwise the whole regex match is
39 /// returned. All capture groups after the first one will be ignored.
40 ///
41 /// @par Example regex:
42 /// Input | Regex | Result
43 /// ----------------------|-------------------|-----------
44 /// Highway 61 revisited | `\d+` | 61
45 /// Highway 61 revisited | `Highway \d+` | Highway 61
46 /// Highway 61 revisited | `Highway (\d+)` | 61
47 /// Highway 61 revisited | `\w+` | Highway
48 /// Highway 61 revisited | `(\w+) (\d+)` | Highway (not an error)
50  : public hext::Cloneable<RegexPipe, StringPipe>
51 {
52 public:
53  /// Constructs a RegexPipe from a boost::regex.
54  ///
55  /// @param regex: A regular expression that is applied to the string.
56  /// See @ref RegexPipeBehavior.
57  explicit RegexPipe(boost::regex regex);
58 
59  /// Filters the string according to the regex given in the constructor.
60  /// See @ref RegexPipeBehavior.
61  std::string transform(std::string str) const override;
62 
63 private:
64  boost::regex regex_;
65 };
66 
67 
68 } // namespace hext
69 
70 
71 #endif // HEXT_REGEX_PIPE_H_INCLUDED
72 
Defines template hext::Cloneable.
Declares hext::StringPipe.
Defines HEXT_PUBLIC and HEXT_PRIVATE.
#define HEXT_PUBLIC
Definition: Visibility.h:26
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:37
Filters a string according to a given regex.
Definition: RegexPipe.h:51
RegexPipe(boost::regex regex)
Constructs a RegexPipe from a boost::regex.
std::string transform(std::string str) const override
Filters the string according to the regex given in the constructor.