C++正则表达式

时间:2021-04-28 05:56:44
 1 #include <regex>
 2 #include <iostream>
 3 #include <stdio.h>
 4 #include <string>
 5 using namespace std;
 6 int main(int argc, char** argv)
 7 {
 8  regex pattern("[[:digit:]]+", regex_constants::extended);
 9  printf("input strings:\n");
10  string buf;
11  while (cin >> buf)
12  {
13   printf("*******\n%s\n********\n", buf.c_str());
14   if (buf == "quit")
15   {
16    printf("quit just now!\n");
17    break;
18   }
19   match_results<string::const_iterator> result;
20   printf("run compare now!  '%s'\n", buf.c_str());
21   bool valid = regex_match(buf, result, pattern);
22   printf("compare over now!  '%s'\n", buf.c_str());
23   if (!valid)
24    printf("no match!\n");
25   else
26    printf("ok\n");
27  }
28  return 0;
29 }