简体   繁体   中英

Finding a number between 2 numbers using regex/boost in c++

I feel like this is a pretty basic question but I did not find a post for it. If you know one please link it below. So what I'm trying to do is look through a string and extract the numbers in groups of 2.

here is my code:

int main() {
        string line = "P112233";
        boost::regex e ("P([0-9]{2}[0-9]{2}[0-9]{2})");
        boost::smatch match;

        if (boost::regex_search(line, match, e))
        {
            boost::regex f("([0-9]{2})"); //finds 11
            boost::smatch match2;
            line = match[0];
            if (boost::regex_search(line, match2, f))
            {
                float number1 = boost::lexical_cast<float>(match2[0]);
                cout << number1 << endl;  // this works and prints out 11.
            }

            boost::regex g("         "); // here I want it to find the 22
            boost::smatch match3;
            if (boost::regex_search(line, match3, g))
            {
                float number2 = boost::lexical_cast<float>(match3[0]);
                cout << number2 << endl;
            }
            boost::regex h("         "); // here I want it to find the 33
            boost::smatch match4;
            if (boost::regex_search(line, match4, h))
            {
                float number3 = boost::lexical_cast<float>(match4[0]);
                cout << number3 << endl;
            }
        }
        else
            cout << "found nothing"<< endl;
    return 0;
}

I was able to get the first number but I have no idea how to get the second(22) and third(33). what's the proper expression I need to use?

As @Cornstalks mentioned you need to use 3 capture groups and then you access them like that:

int main() 
{
    std::string line = "P112233";
    boost::regex e("P([0-9]{2})([0-9]{2})([0-9]{2})");
    boost::smatch match;

    if (boost::regex_search(line, match, e))
    {
        std::cout << match[0] << std::endl; // prints the whole string
        std::cout << match[1] << ", " << match[2] << ", " << match[3] << std::endl;
    }

    return 0;
}

Output:

P112233
11, 22, 33

I don't favour regular expressions for this kind of parsing. The key point being that the numbers are still strings when you're done with that hairy regex episode.

I'd use Boost Spirit here instead, which parses into the numbers all at once, and you don't even have to link to the Boost Regex library either, because Spirit is header-only.

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <iostream>

namespace qi = boost::spirit::qi;
static qi::int_parser<int, 10, 2, 2> two_digits;

int main() {
    std::string const s = "P112233";

    std::vector<int> nums;
    if (qi::parse(s.begin(), s.end(), "P" >> *two_digits, nums))
    {
        std::cout << "Parsed " << nums.size() << " pairs of digits:\n";
        for(auto i : nums)
            std::cout << " * " << i << "\n";
    }
}


Parsed 3 pairs of digits:
 * 11
 * 22
 * 33

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM