代码片段

 1 //http://solarianprogrammer.com/2011/10/12/cpp-11-regex-tutorial/
 2 #include <iostream>
 3 #include <regex>
 4 #include <string>
 5 
 6 using namespace std;
 7 using namespace std::tr1;
 8 int main()
 9 {
10     string input;
11     //regex integer("(\\+|-)?[[:digit:]]+");
12     regex integer("(\\+|-)?[\\d]+");
13     //regex integer("(\\+|-)?[[:digit:]]+");
14     //As long as the input is correct ask for another number
15     while(true)
16     {
17         cout<<"Give me an integer!"<<endl;
18         cin>>input;
19         //Exit when the user inputs q
20         if(input=="q")
21             break;
22         if(regex_match(input,integer))
23             cout<<"integer"<<endl;
24         else
25         {
26             cout<<"Invalid input"<<endl;
27         }
28     }
29     //return 0;
30 }
c++使用正则表达式

 

posted on 2013-09-10 21:53  zhiying678  阅读(96)  评论(0编辑  收藏  举报

导航