summaryrefslogtreecommitdiff
path: root/regex-match-example.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'regex-match-example.cpp')
-rw-r--r--regex-match-example.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/regex-match-example.cpp b/regex-match-example.cpp
new file mode 100644
index 0000000..ab71e66
--- /dev/null
+++ b/regex-match-example.cpp
@@ -0,0 +1,42 @@
+// regex_match example
+// NOTE:
+// std::regex_match matches the ENTIRE string
+// (as if with '^' and '$' anchors).
+// Use std::regex_search to match partial strings.
+#include <iostream>
+#include <string>
+#include <regex>
+#include <cassert>
+
+using namespace std;
+
+int main ()
+{
+ // should match - regex accepts the entire string
+ bool b = regex_match("chromium",regex("chr.*"));
+ assert(b);
+
+ // should not match - regex does not accept entire
+ // string, yet 'regex_match' requires entire input to match
+ // (unlike regex_search).
+ b = regex_match("chromium",regex("chr"));
+ assert (!b);
+
+
+ // match on 'char*' - use 'cmatch'
+ const char cstr[] = "hello world 42";
+ cmatch cm;
+ regex re(".*?(\\d+)");
+ b = regex_match (cstr,cm,re);
+ assert(b);
+ cout << "const char - matched number: " << cm[1] << endl;
+
+ // match on 'string' - use 'smatch'
+ const string s("hello world 41");
+ smatch sm;
+ b = regex_match (s,sm,re);
+ assert(b);
+ cout << "string - matched number: " << sm[1] << endl;
+
+ return 0;
+}