#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】关注后,获取更多,我们一起学习交流。
关于公众号的描述访问如下链接