• regex_iterator


    绝对不能在循环中通过regex_search获取模式在源字符串所有的实例;应该改为regex_iterator或则regex_token_iterator,一般情况下需要位一个特定的容器来指定一个尾迭代器,但是在std::regex_iterator 里边直接调用构造函数就会生成一个尾迭代器;例如:
    std::regex_iterator end;

    而不需要:std::regex_iterator end(std::end(str));

    但是为了遍历全部的源字符串,我们需要这样来制定一个首迭代器;

    Std::regex_iterator iter(std::begin(str),std::end(str), r );

    其中的r是正则表达式;

    接下来提取输入的单词

    #include <iostream>
    #include <regex>
    int main() {
    
        std::regex r("[\w]+");
        std::string str;
        while(true)
        {
            if(!std::getline(std::cin,str) || str == "q")
            {
                break;
            }else
            {
                //typedef regex_iterator<string::const_iterator>  sregex_iterator;
                const std::sregex_iterator end;
                for(std::sregex_iterator iter(std::begin(str),std::end(str),r);iter != end;++iter)
                {
                    std::cout <<  """ << *iter->begin()  << """ << std::endl;
                }
            }
        }
        return 0;
    }

    输入:

    djsf jkdfn , klj 

    输出:

    "djsf"
    "jkdfn"
    "klj"

  • 相关阅读:
    假期第九天
    假期第八天
    8.9周总结
    构建之法阅读笔记之三
    构建之法阅读笔记之二
    构建之法阅读笔记之一
    03人月神话阅读笔记之一
    手机输入法使用评价
    十天冲刺-第十天
    十天冲刺-第九天
  • 原文地址:https://www.cnblogs.com/boost/p/10426086.html
Copyright © 2020-2023  润新知