libhext: C++ Library Documentation  1.0.12-3ea013c
RegexReplacePipe.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_REPLACE_PIPE_H_INCLUDED
16 #define HEXT_REGEX_REPLACE_PIPE_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::RegexReplacePipe
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 /// Replaces a string within a string according to a given regex.
34 ///
35 /// @par Example:
36 /// Use backreferences to address capture groups:
37 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38 /// RegexReplacePipe r(boost::regex("^(\\w+) (\\\w+)$"), "$2 $1");
39 /// r.transform("first second"); // "second first"
40 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42  : public hext::Cloneable<RegexReplacePipe, StringPipe>
43 {
44 public:
45  /// Constructs a RegexReplacePipe.
46  ///
47  /// @param regex: A regular expression that is applied to the string.
48  /// @param str: The string that will replace the portion matching regex.
49  RegexReplacePipe(boost::regex regex, std::string str);
50 
51  /// Replaces a string within str according to the regex given in the
52  /// constructor.
53  std::string transform(std::string str) const override;
54 
55 private:
56  boost::regex regex_;
57  std::string replace_;
58 };
59 
60 
61 } // namespace hext
62 
63 
64 #endif // HEXT_REGEX_REPLACE_PIPE_H_INCLUDED
65 
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
Replaces a string within a string according to a given regex.
std::string transform(std::string str) const override
Replaces a string within str according to the regex given in the constructor.
RegexReplacePipe(boost::regex regex, std::string str)
Constructs a RegexReplacePipe.