2

I have just started using boost C++ libraries for some parser work. I would like to use some help on the following:

  1. Matching an 8-bit hexadecimal number. I have tried: char_("0-9a-fA-F") which matches only one hexadecimal digit. I've also tried using: *char_("0-9a-fA-F"), but it is also not working

  2. Matching any string with underscore characters? For example, aBCd_Efgh

Alan Moore
  • 73,866
  • 12
  • 100
  • 156

2 Answers2

3

Since indeed char_ is from boost::spirit::qi you'll want to simply use the integer parser:

This is easily used to parse hexadecimals:

qi::int_parser<unsigned char, 16, 2, 2> hex_byte;

will match and parse exactly 2 hexadecimal digits in succession.

sehe
  • 374,641
  • 47
  • 450
  • 633
0

If you are using PCRE, i.e., initialized your regex like boost::regex e1(my_expression) or boost::regex e2(my_expression, boost::regex::perl), then:

  1. You can specify a quantifier using brackets ({min,max}), so a{3,10} would match 3 to 10 a characters. You can apply that to a group, like [0-9a-fA-F]{1,2}
  2. A group expression would do fine: [0-9a-zA-Z_]+

If you are using POSIX, the differences are:

  1. You need to escape the brackets \{min,max\}
  2. Nothing.
Bruno Ferreira
  • 1,621
  • 12
  • 19