加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则匹配

发布时间:2020-12-14 06:00:56 所属栏目:百科 来源:网络整理
导读:#include iostream #include string #include regex using namespace std; int main() { //regex_match匹配整个字符串 //reg1匹配大小写字母,不能匹配空格 regex reg1("w+day"); smatch r1; string s1 = "Sat123urdayday"; cout boolalpha regex_match(s1,

#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main()
{
//regex_match匹配整个字符串
//reg1匹配大小写字母,不能匹配空格
regex reg1("w+day");
smatch r1;
string s1 = "Sat123urdayday";
cout << boolalpha << regex_match(s1,r1,reg1) << endl; //true
cout << "s1匹配结果:" << r1.str() << endl;

//regex_match只返回第一个匹配结果
//string s2 = "saturday and sunday"; //false
string s2 = "saturaday ndsunday"; //true
smatch r2;
cout << boolalpha << regex_match(s2,r2,reg1) << endl; //false
cout << "s2匹配结果:" << r2.str() << " 1234654" << endl; //结果为空


smatch rr1;
smatch rr2;
cout << boolalpha << regex_search(s1,rr1,reg1) << endl;
cout << "s1匹配结果:" << rr1.str() << endl;
cout << boolalpha << regex_search(s2,rr2,reg1) << endl;
cout << "s2匹配结果:" << rr2.str() << " asdasd5" << endl;
cout << endl;


//使用iterator返回多个匹配结果
//结果要用->
cout << "iterator结果:" << endl;
sregex_iterator it(s2.begin(),s2.end(),reg1);
sregex_iterator end;
for(; it != end; ++it)
{
cout << it->str() << endl;
//cout << *it << endl; 错误
}

cout << "token_iterator结果:" << endl;
sregex_token_iterator tit(s2.begin(),reg1);
sregex_token_iterator tend;
for(; tit != tend; ++tit)
{
cout << tit->str() << endl;
cout << *tit << endl;
}

//子表达式匹配
regex reg2("(d{1,3}):(d{1,3})");
string ip = "0:11:222:333";
smatch m;
regex_match(ip,m,reg2);
cout << "输出:str()" << endl;
cout << m.str() << endl;
cout << m.str(1) << endl;
cout << m.str(2) << endl;
cout << m.str(3) << endl;
cout << m.str(4) << endl;

cout << "输出:[i]" << endl;
cout << m[0] << endl;
cout << m[1] << endl;
cout << m[2] << endl;
cout << m[3] << endl;
cout << m[4] << endl;

//输出结果同上
//regex_search(ip,str2);
cout << endl;
string ip2 = "0:11:222:333 4:55:66:7";
sregex_iterator ip_it(ip2.begin(),ip2.end(),reg2);
sregex_iterator ip_end;
for(; ip_it != ip_end; ++ip_it)
{
cout << ip_it->str() << endl;
cout << ip_it->str(1) << endl;
cout << ip_it->str(2) << endl;
cout << ip_it->str(3) << endl;
cout << ip_it->str(4) << endl;
}

regex regx("d{18}");
regex regx1("d{17}+[X]");
smatch sma;
string str = "41282519980203733X";
cout << boolalpha << regex_match(str,sma,regx1) << endl; //true
cout << "s1匹配结果:" << sma.str() << endl;

return 0;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读