0

I have a text with numbers like "25,6 km/h". I tried to parse it with boost spirit qi.

numeric_value_expression = qi::double_ >> "km" >> -(string("/")) >> "h";

But this works only for US-formatted numbers like "25.6 km/h" not with commas. Is there a property or possibility to workaround this?

Spide
  • 89
  • 5
  • 1
    Probably with custom [`RealPolicies`](http://www.boost.org/doc/libs/1_62_0/libs/spirit/doc/html/spirit/qi/reference/numeric/real.html#spirit.qi.reference.numeric.real._code__phrase_role__identifier__realpolicies__phrase___code_) -- provide your own `RP::parse_dot(f, l)` implementation. – Dan Mašek Dec 08 '16 at 13:53
  • OK, thank you, I will look into it. – Spide Dec 08 '16 at 14:46
  • 1
    @Spide this answer essentially already has it (among other things) https://stackoverflow.com/questions/32787145/parsing-strings-with-value-modifiers-at-the-end/32856174#32856174 – sehe Dec 08 '16 at 16:58

1 Answers1

0

If you don't care about things like exponential notation, you could write your own parser:

namespace qi = boost::spirit::qi;

std::string s = "2,123";

double dSpeed = 0.0;

qi::rule<std::string::iterator, double()> fraqDig =
    qi::char_("0-9")[qi::_val = qi::_1 - '0'] >> -(fraqDig[qi::_val += qi::_1 / 10.0]);

qi::rule<std::string::iterator, double()> myFloat =
    qi::int_[qi::_val = qi::_1] >> -(',' >> fraqDig[qi::_val += qi::_1 / 10.0]);

qi::parse(s.begin(), s.end(), myFloat, dSpeed);
Boris Glick
  • 202
  • 1
  • 4