Larytet
Larytet

Reputation: 646

How to get index of the matching group

I want to list all matching groups in the input. For example, I want to print all browsers I care about in a User-Agent HTTP header. One match is enough. Is there a way to get rid of the inner for loop in the code below. I looked in the boost/regex/sub_match.hpp and I have no ideas.

#include <string> 
#include <iostream> 
#include <boost/regex.hpp> // Boost 1.59, no C++14 for me

enum BrowserType
{
    FIREFOX = 0 ,
    CHROME,
    SAFARI,
    OPERA,
    IE,
    EDGE,
    OTHER,
};

const boost::regex BROWSERS_REGEX("(Firefox)|(Chrome)|(Safari)|(Opera)|(MSIE)|(Edge)|(Trident)");

int main()
{
        // I expect two matches here CHROME and SAFARI
        std::string input("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94");
        boost::sregex_iterator res(input.begin(), input.end(), BROWSERS_REGEX);
        boost::sregex_iterator end;
        for(; res != end; ++res)
        {
                // elude copy here ?
                boost::smatch what = *res;
                // Can I know the index of the matching group w/o 'for'?
                for (int type = 0;type < OTHER;type++) 
                {
                        int groupIndex = type+1;
                        if (what[groupIndex].matched) 
                                std::cout << (BrowserType)type << ",";
                }
                std::cout << "\n";

        }

        return 0;
}

Important update. The strstr() based code is the fastest approach for strings shorter than 1K bytes

    const char *input("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94");
    const char *browsers[OTHER+1] = {"Firefox", "Chrome", "Safari", "Opera", "MSIE", "Edge", "Trident"};
    int i = 0;
    for (const char **browser = &browsers[0];browser <= &browsers[OTHER];browser++, i++)
    {
            if (strstr(input, *browser))
            {
                    groupindex = i;
            }
    }

Upvotes: 1

Views: 102

Answers (1)

sehe
sehe

Reputation: 393593

You can use named captures.

I just used it earlier today: While doing url encoding , The std::regex_replace doesn't work properly for character "+" . Can some body help me out?

In your case, though, I'd use qi::symbols:

struct browser_type_sym : boost::spirit::qi::symbols<char, BrowserType> {
    browser_type_sym() {
        this->add
            ("Firefox", FIREFOX)
            ("Chrome",  CHROME)
            ("Safari",  SAFARI)
            ("Opera",   OPERA)
            ("MSIE",    IE)
            ("Edge",    EDGE)
            ("Trident", OTHER);
    }
} static const browser_type;

You can simply use it with any container of BrowserType:

template <typename Types>
bool extract_browser_ids(std::string const& userAgent, Types& into) {
    using boost::spirit::repository::qi::seek;
    return parse(userAgent.begin(), userAgent.end(), *seek [ browser_type ], into);
}

Using vector<BrowserType>

As you will see, if you use vector<BrowserType it will retain order and duplicates:

Live On Coliru

int main() {
    for (std::string const input : {
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94",
            "Chrome; Opera; Chrome again!"
    }) {
        std::vector<BrowserType> types;
        extract_browser_ids(input, types);
        for(auto type : types)
            std::cout << type << ",";

        std::cout << "\n";
    }
}

Prints:

2,1,
1,3,1,

Using set<BrowserType>

When using set<> it will order and de-duplicate:

Live On Coliru

    std::set<BrowserType> types;

Prints:

1,2,
1,3,

Case Insensitivity

Just to show minor tweaks:

struct browser_type_sym : boost::spirit::qi::symbols<char, BrowserType> {
    browser_type_sym() {
        this->add
            ("firefox", FIREFOX)
            ("chrome",  CHROME)
            ("safari",  SAFARI)
            ("opera",   OPERA)
            ("msie",    IE)
            ("edge",    EDGE)
            ("trident", OTHER);
    }
} static const browser_type;

template <typename Types>
bool extract_browser_ids(std::string const& userAgent, Types& into) {
    using boost::spirit::repository::qi::seek;
    using boost::spirit::qi::no_case;
    return parse(userAgent.begin(), userAgent.end(), *seek [ no_case [ browser_type ] ], into);
}

Now case doesn't matter: Live On Coliru

Alternative: Boost Xpressive

Similarly you can use Boost Xpressive, which is slightly closer to the regex approach (though internally it still builds a trie of strings from the map).

The semantic actions require more manual labour, making it also less generic (it won't work with std::set without changes, unlike the Spirit Qi approach already shown).

Nevertheless, for completeness:

Live On Coliru

std::map<std::string, BrowserType> s_browser_type_map {
        {"Firefox", FIREFOX},
        {"Chrome",  CHROME},
        {"Safari",  SAFARI},
        {"Opera",   OPERA},
        {"MSIE",    IE},
        {"Edge",    EDGE},
        {"Trident", OTHER},
    };

#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>

template <typename Types>
void extract_browser_ids(std::string const& userAgent, Types& into) {
    using namespace boost::xpressive;

    placeholder<Types> _result;
    sregex type = (a1 = s_browser_type_map) [ push_back(_result, a1) ];

    for (sregex_iterator it(userAgent.begin(), userAgent.end(), type, let(_result=into)),
            end; it != end; ++it) 
    { } // all side-effects in the semantic action
}

Which prints the same output as the vector<> Spirit example:

2,1,
1,3,1,

Upvotes: 1

Related Questions