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

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值