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

原文地址:https://www.cnblogs.com/boost/p/10425506.html