59.C++与正则表达式

  • regex_match 整个字符串是否匹配 (通过cmatch存储匹配的结果),match[0]代表整个匹配序列,match[1]代表第1个匹配后的子序列,match[2]代表第2个匹配后的子序列

代码示例:

 1    regex reg("^select ([a-zA-Z]*) from ([a-zA-Z]*) to ([a-zA-Z]*)$");
 2     cmatch what;//匹配的词语检索出来
 3     bool isit = regex_match("select id from admin to adminmin", what, reg);
 4     if (isit)
 5     {
 6         cout << what.size() << endl;
 7         //输出匹配的信息
 8         for (int i = 0; i < what.size(); i++)
 9         {
10             cout << i << "  " << what[i].first << endl;
11         }
12 
13         cout << "匹配" << endl;
14     }
15     else
16     {
17         cout << "不匹配" << endl;
18     }
 1   //^开头 ()组 []可供选择的集合 {}几次 $结尾 |或者
 2     regex reg("^(1[3|4|5|7|8][0-9]{9})");
 3     string str1;
 4     cin >> str1;
 5     bool isit = regex_match(str1, reg);//进行匹配,不需要返回结果
 6     if (isit)
 7     {
 8         cout << "OK" << endl;
 9     }
10     else
11     {
12         cout << "not OK" << endl;
13     }

 

regex_search 整个字符串进行查找判断是否含有指定数据类型

 1 void main()
 2 {
 3     //代表查找数字
 4     regex reg("\\d+|([a-z])+|([A-Z]+)");
 5     char str[50] = "HELLO hello 1234 hello 111---134--324-@@@@";
 6     //查找匹配的数字
 7     bool isOK = regex_search(str, reg);
 8     if (isOK)
 9         {
10             cout << "含有指定数据类型" << endl;
11          }
12     cin.get();
13 }

 

 

 

  • regex_replace 按照正则表达式进行替换
    1     cmatch match;
    2     //代表查找数字
    3     regex reg("\\d+");
    4     char str[50] = "hello 1234 hello 111---134";
    5     //把数字都换成12345
    6     cout << regex_replace(str, reg, "12345") << endl;
    7     cin.get();
  • regex_match与smatch存放正则表达式单匹配的字符串
     1 //日期正则表达式
     2     regex reg("^(\\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][0-9]|3[0-1])$");
     3     string str1;
     4     getline(cin,str1);
     5     cout << str1 << endl;
     6     smatch m;//帮助转换
     7     if (regex_match(str1, m, reg))
     8     {
     9         //m[0]忽略
    10         int year = atoi(m[0].str().c_str());//汇总全部
    11         int month = atoi(m[1].str().c_str());//1,2,3挨个
    12         int day = atoi(m[2].str().c_str());
    13         cout << "OK" << endl;
    14         cout << year << month << day << endl;
    15     }
    16     else
    17     {
    18         cout << "NO" << endl;
    19     }
  • sregex_token_iterator 根据特定字符拆分数据,并把拆分的数据存放在vector数组中
     1 //正则表达式拆分
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <regex>
     5 #include <string>
     6 #include <vector>
     7 using namespace std;
     8 
     9 void main()
    10 {
    11     //通过中间所出现的字符截断 前提是前后对称 []里面是截断的字符 +表示一个或多个
    12     regex reg("\\s*[,#;' ']+\\s*");
    13     string str;
    14     getline(cin, str);
    15     //迭代器,拆分字符串
    16     sregex_token_iterator end;
    17     vector<string> myv;
    18     //                              开始            结束     正则表达式检索  -1检索结束
    19     for (sregex_token_iterator it(str.begin(), str.end(),   reg,              -1); it != end; it++)
    20     {
    21         //cout << *it << endl;
    22         //把拆分的数据存储到vector数组中
    23         myv.push_back(*it);
    24     }
    25     //打印存储的结果
    26     for (auto i : myv)
    27     {
    28         cout << i << endl;
    29     }
    30 
    31     cin.get();
    32 }
posted @ 2018-03-13 23:36  喵小喵~  阅读(253)  评论(0编辑  收藏  举报