Iterating Over the Matches Within An MFC String

| Home | Libraries | People | FAQ | More

##### Iterating Over the Matches Within An MFC String

The following helper functions are provided to ease the conversion from an MFC/ATL string to a `regex_iterator` or `regex_token_iterator`:

##### regex_iterator creation helper

    template <class charT>
    regex_iterator<charT const*>
       make_regex_iterator(
          const ATL::CSimpleStringT<charT>& s,
          const basic_regex<charT>& e,
          ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);

Effects: returns `regex_iterator(s.GetString(), s.GetString() + s.GetLength(), e, f);`

Example:

    void enumerate_links(const CString& html)
    {
       // enumerate and print all the  links in some HTML text,
       // the expression used is by Andew Lee on www.regxlib.com:
       boost::tregex r(
          __T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+"
              "(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*"
              "(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
       boost::tregex_iterator i(boost::make_regex_iterator(html, r)), j;
       while(i != j)
       {
          std::cout << (*i)[1] << std::endl;
          ++i;
       }
    }

##### regex_token_iterator creation helpers

    template <class charT>
    regex_token_iterator<charT const*>
       make_regex_token_iterator(
          const ATL::CSimpleStringT<charT>& s,
          const basic_regex<charT>& e,
          int sub = 0,
          ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);

Effects: returns `regex_token_iterator(s.GetString(), s.GetString() + s.GetLength(), e, sub, f);`

    template <class charT>
    regex_token_iterator<charT const*>
       make_regex_token_iterator(
          const ATL::CSimpleStringT<charT>& s,
          const basic_regex<charT>& e,
          const std::vector<int>& subs,
          ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);

Effects: returns `regex_token_iterator(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);`

    template <class charT, std::size_t N>
    regex_token_iterator<charT const*>
       make_regex_token_iterator(
          const ATL::CSimpleStringT<charT>& s,
          const basic_regex<charT>& e,
          const int (& subs)[N],
          ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);

Effects: returns `regex_token_iterator(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);`

Example:

    void enumerate_links2(const CString& html)
    {
       // enumerate and print all the  links in some HTML text,
       // the expression used is by Andew Lee on www.regxlib.com:
       boost::tregex r(
             __T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+"
                 "(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*"
                 "(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
       boost::tregex_token_iterator i(boost::make_regex_token_iterator(html, r, 1)), j;
       while(i != j)
       {
          std::cout << *i << std::endl;
          ++i;
       }
    }