c++11:正则表达式

#include <iostream>
#include<regex>//正则表达式头文件
using namespace std;

//regex_search 检索
//regex_replace  将检索到的对象进行替换替换
//match 是否匹配
void main()
{
	regex reg("^select ([A-Za-z]*) from ([A-Za-z]*)$");
	//判断表达式是否合法
	cmatch what;
	bool isit = regex_match("select id from admin", what, reg);
	if (isit)
	{
		for (int i = 0; i !=what.size(); i++)
		{
			cout << what[i + 1].first << "   ";
		}
		cout <<endl<< "匹配,合法" << endl;
	}
	else
	{
		cout <<endl<< "不匹配,error" << endl;
	}
	cin.get();
}

//正则表达式实现字符串的替换
void main()
{
	cmatch match;
	regex reg("rocket");
	char str1[55] = "rocketrocket19roc9ket2he1llo1992 rocket8848";
	cout << str1 << endl;
	cout << regex_replace(str1, reg, "黑夜") << endl;
	cin.get();
}

//匹配时间
void  main()
{
	regex reg("(^\\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][0-9]|3[0-1])$");
	string str1;
	cout << "请输入日期(格式:2016/06/16):";
	getline(cin, str1);//遇到\n结束
	cout << str1 << endl;
	smatch m;
	if (regex_match(str1,m, reg))
	{
		cout <<endl<< m[0].str().c_str() << endl;
		cout << m[1].str().c_str() << endl;
		cout << m[2].str().c_str() << endl;
		cout << m[3].str().c_str() << endl;

		int year = atoi(m[0].str().c_str());
		int month = atoi(m[1].str().c_str());
		int day = atoi(m[2].str().c_str());
		cout << year<< month << day << endl;
	}
	else
		cout << "no" << endl;

	cin.get();
}
<img src="https://img-blog.youkuaiyun.com/20150823012237677?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

更多资料 · 微信公众号搜索【CTO Plus】关注后,获取更多,我们一起学习交流。

关于公众号的描述访问如下链接


关于Articulate“做一个知识和技术的搬运工。做一个终身学习的爱好者。做一个有深度和广度的技术圈。”一直以来都想把专业领域的技https://mp.weixin.qq.com/s/0yqGBPbOI6QxHqK17WxU8Q

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveRocket

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值