Traverse a search using boost Regex regex_search

Source: Internet
Author: User
Tags regex expression

In the regex_search function, the first matching result found is saved to a Smatch class.

However, if there are multiple matching results in the search string, you need to implement it yourself.

In Smatch, there are two members, the official documents are as follows:

Iterator First:

An iterator denoting the position of the the match.

Iterator second

An iterator denoting the position of the end of the match.

Therefore, the following method can be used to get a traversal search:

[CPP]View Plaincopy
  1. #include <string>
  2. #include <iostream>
  3. #include <boost\regex.hpp>
  4. int main ()
  5. {
  6. std::string str = "192.168.1.1";
  7. Boost::regex expression ("\\d+");
  8. Boost::smatch what;
  9. Std::string::const_iterator start = Str.begin ();
  10. Std::string::const_iterator end = Str.end ();
  11. While (Boost::regex_search (Start, end, what, expression))
  12. {
  13. Std::cout << what[0] << Std::endl;
  14. start = What[0].second;
  15. }
  16. return 0;
  17. }

The results are as follows:

[Plain]View Plaincopy
    1. 192
    2. 168
    3. 1
    4. 1

In boost, an iterator is also provided, named: Sregex_iterator, and the default constructor generates an end iterator. Use the following:

[CPP]View Plaincopy
  1. #include <string>
  2. #include <iostream>
  3. #include <boost\regex.hpp>
  4. int main ()
  5. {
  6. std::string str = "192.168.1.1";
  7. Boost::regex expression ("\\d+");
  8. Boost::sregex_iterator It (Str.begin (), str.end (), expression);
  9. Boost::sregex_iterator end;
  10. For (; it! = end; ++it)
  11. Std::cout << *it << Std::endl;
  12. return 0;
  13. }

The effect is the same as the previous example. If you do not need to traverse, only need to match, that is simpler: Boost::regex reg (Szreg);
BOOL R=boost::regex_match (SZSTR, Reg) or need to be placed in a cmatch: {Boost::cmatch mat;
Boost::regex Reg ("\\d+"); Find numbers in a string
if (Boost::regex_search (Szstr, Mat, Reg))
{
cout << "searched:" << mat[0] << Endl;
}
}

Traverse a search using boost Regex regex_search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.