您的位置:首页 > 其它

regex_search()和getline(cin,str)函数,以及对其中正则表达式的解释

2016-03-08 11:52 856 查看
#include<iostream>
#include<regex>
#include<string>
using namespace std;
int main(int argc, char argv[])
{
//进行注释语句的匹配
regex r("//\\s*(.+)");
//注释正则表达式:
//1://为注释语句中的“//”
//2:\\s为空格,换行符,制表符等
//3:\\s*为有0个或者多个空格,换行符,制表符等
//4:(.+)该语句可匹配对个字符,数字等,其中.相当于是通配符(大小写字母,数字等)
while (true)
{
cout << "输入注释语句(q=quit):";
string str;
//getline(cin,str)该函数的原型是   ifstream& getline(istream& in,string& str)
//特德返回值是in,是一个引用,不可能是NULL(所以!getline(cin,str)恒为true)
//所以如果是if(!getline(cin,str))是不可以的  应该换成str==""
getline(cin, str);
if (str==""|| str == "q")
{
break;
}
smatch sm;
if (regex_search(str, sm, r))
{
cout << "find string:" << sm[1] << endl;
}
else
{
cout << "no find" << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: