• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
p-boost-q
博客园    首页    新随笔    联系   管理    订阅  订阅
regex_match

原型:bool regex_match(InputSequence[,MatchResults] , Regex[ , Flags]);

当模式匹配整个输入序列成功时,返回的是true,否则返回false;

参数说明:

1.InputSequence可以是:源字符串的首位迭代器,也可以是字符串;

2.MatchResult时可选参数,是match_result的引用,当regex_match返回的是false, MatchResult就是match_result::empty()或则match_result::size();当regex_match()返回的是true时,MatchResult保存的是匹配的结果;

3. Regex是正则表达式;

#include <iostream>
#include <regex>
int main() {
    std::regex r("\\d{4}/(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][0-9]|3[0-1])");
    std::string str;
    while(true)
    {
        if(!std::getline(std::cin,str) || str == "q")
        {
            break;
        } else
        {
            std::smatch match;
            if(std::regex_match(str,match,r))
            {
                std::cout << "vaild argument" << std::endl;
                std::cout << match[0] << std::endl;
                std::cout << match[1] << std::endl;
                std::cout << match[2] << std::endl;
            }else
            {
                std::cout << "invaild argument" << std::endl;
            }

        }
    }
    return 0;
}

运行结果:

1996/2/21
vaild argument
1996/2/21
2
21

posted on 2019-02-24 10:45  p-boost-q  阅读(525)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3