All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
RegexReplacePipe.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_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 
24 #include <string>
25 
26 #include <boost/regex.hpp>
27 
28 
29 namespace hext {
30 
31 
32 /// Replaces a string within a string according to a given regex.
33 ///
34 /// @par Example:
35 /// Use backreferences to address capture groups:
36 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37 /// RegexReplacePipe r(boost::regex("^(\\w+) (\\\w+)$"), "$2 $1");
38 /// r.transform("first second"); // "second first"
39 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 class RegexReplacePipe final : public hext::Cloneable<RegexReplacePipe, StringPipe>
41 {
42 public:
43  /// Constructs a RegexReplacePipe.
44  ///
45  /// @param regex: A regular expression that is applied to the string.
46  /// @param str: The string that will replace the portion matching regex.
47  RegexReplacePipe(boost::regex regex, std::string str);
48 
49  /// Replaces a string within str according to the regex given in the
50  /// constructor.
51  std::string transform(std::string str) const override;
52 
53 private:
54  boost::regex regex_;
55  std::string replace_;
56 };
57 
58 
59 } // namespace hext
60 
61 
62 #endif // HEXT_REGEX_REPLACE_PIPE_H_INCLUDED
63 
Replaces a string within a string according to a given regex.
Declares hext::StringPipe.
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
std::string transform(std::string str) const override
Replaces a string within str according to the regex given in the constructor.
Defines template hext::Cloneable.
RegexReplacePipe(boost::regex regex, std::string str)
Constructs a RegexReplacePipe.