#include #include namespace qi = boost::spirit::qi; using qi::omit; using qi::repeat; using std::cout; using std::endl; using qi::string; using qi::char_; typedef qi::rule strrule_t; void test(const std::string input, strrule_t rule) { std::string target; std::string::const_iterator i = input.begin(), ie = input.end(); if (qi::parse(i, ie, rule, target)) { cout << "Success: '" << target << "'" << endl; } else { cout << "Failed to match." << endl; } } int main() { strrule_t shortHexValue = repeat(4)[char_("a-fA-F0-9")]; strrule_t longHexValue = repeat(8)[char_("a-fA-F0-9")]; strrule_t hexValue1 = omit[string("0x")] >> ( longHexValue | shortHexValue ); strrule_t hexValue2 = omit[string("0x")] >> ( shortHexValue | longHexValue ); test( "0x123FAA22", hexValue1 ); test( "0x12AA", hexValue1 ); test( "0x123FAA22", hexValue2 ); test( "0x1A22", hexValue2 ); return 0; }