zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    20200929-git地址
    20200917-1 每周例行报告
    20200917-2 词频统计
    20200917-3 白名单
    20200910-1 每周例行报告
    20200910-2 博客作业
    20190919-6 四则运算试题生成,结对
    20190919-3 效能分析
    20190919-2 功能测试
    20190912-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/boost/p/10425506.html
Copyright © 2011-2022 走看看