summaryrefslogtreecommitdiff
path: root/regex-replace-example.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'regex-replace-example.cpp')
-rw-r--r--regex-replace-example.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/regex-replace-example.cpp b/regex-replace-example.cpp
new file mode 100644
index 0000000..08c2420
--- /dev/null
+++ b/regex-replace-example.cpp
@@ -0,0 +1,38 @@
+// regex_replace example
+#include <iostream>
+#include <string>
+#include <regex>
+#include <iterator>
+
+using namespace std;
+
+int main ()
+{
+ string s ("hello wherever wholly heroic howlling herring");
+
+ // find words starting with 'he'.
+ // words containing 'he' not at the beginning should not be replaced.
+ regex e ("\\bhe");
+
+ // replace ALL 'he' with 'HE'
+ // (only in
+ cout << "upper-case HE (all): "
+ << regex_replace(s,e,"HE")
+ << endl;
+
+ // replace just one (the first) 'he with 'HE'
+ cout << "upper-case HE (one): "
+ << regex_replace(s,e,"HE", regex_constants::format_first_only)
+ << endl;
+
+
+ string s2("hello world");
+ // find two words separated by spaces
+ regex e2("(\\w+)\\s+(\\w+)");
+ // Replace their order
+ cout << "swapped words: "
+ << regex_replace (s2,e2,"$2 $1")
+ << endl;
+
+ return 0;
+}