Use boost RegEx's regex_search for traversal and search

Source: Internet
Author: User
Tags regex expression

In the regex_search function, the first matching result is saved to an smatch class.

However, if the search string contains multiple matching results, you must implement it yourself.

In smatch, there are two members. The official documents are as follows:

Iterator first:

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

Iterator second

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

Therefore, use the following method to obtain the traversal search:

#include <string>#include <iostream>#include <boost\regex.hpp>int main(){    std::string str = "192.168.1.1";    boost::regex expression("\\d+");    boost::smatch what;std::string::const_iterator start = str.begin();std::string::const_iterator end = str.end();    while ( boost::regex_search(start, end, what, expression) )    {std::cout << what[0] << std::endl;start = what[0].second;    }return 0;}

The result is as follows:

19216811

Use boost RegEx's regex_search for traversal and 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.