Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4333026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:20:13+00:00 2026-05-21T10:20:13+00:00

I need to parse 1-line expressions containing key/value pairs and key/subexpression pairs, like: 123=a

  • 0

I need to parse 1-line expressions containing key/value pairs and key/subexpression pairs, like:

123=a 456=b 789=(a b c) 111=((1=a 2=b 3=c) (1=x 2=y 3=z) (123=(x y z))) 666=evil

To make the parser simpler, I’m willing to do the parsing in several steps, separating the first-level tags (here 123, 456, 789, 111 and 666, and then parsing their content in another step.
Here 789’s value would be "a b c", 111’s value would be (1=a 2=b 3=c) (1=x 2=y 3=z) (123=(x y z)).

But grammars beat me at this point so I can’s figure out a way to get the expressions between matching parenthesis. All I get for 111 is (1=a 2=b 3=c, which ends at the first closing parenthesis.

I found this handy example and tried to use it, with no success:

#include <map>
#include <string>
#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace qi = boost::spirit::qi;

void main()
{
    auto                                                                   value = +qi::char_("a-zA-Z_0-9");
    auto                                                                   key   =  qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
    qi::rule<std::string::iterator, std::pair<std::string, std::string>()> pair  =  key >> -('=' >> value);
    qi::rule<std::string::iterator, std::map<std::string, std::string>()>  query =  pair >> *((qi::lit(';') | '&') >> pair);

    std::string input("key1=value1;key2;key3=value3");  // input to parse
    std::string::iterator begin = input.begin();
    std::string::iterator end = input.end();

    std::map<std::string, std::string> m;        // map to receive results
    bool result = qi::parse(begin, end, query, m);   // returns true if successful
}

How I can do that ?

Edit: I found the example at http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:20:14+00:00Added an answer on May 21, 2026 at 10:20 am

    You could write it as:

    qi::rule<std::string::iterator, std::pair<std::string, std::string>()> pair = 
            key >> -(
               '=' >> ( '(' >> raw[query] >> ')' | value )
            )
        ;
    

    which will store all embedded queries as values (strings) associated with the key. This will drop the parenthesis from the stored values, though. If you still want to have the parenthesis stored in the returned attributes, use this:

    qi::rule<std::string::iterator, std::pair<std::string, std::string>()> pair = 
            key >> -(
               '=' >> ( raw['(' >> query >> ')'] | value )
            )
        ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse a command line like script.rb <mandatory filename> [options] with optparse
I need to parse a line from cmd that looks like this SOME WHITE
I need to parse a command line string in to the argv format so
I need to parse an CSV file using AWK. A line in the CSV
I'm using pyparsing to parse documents containing text in which the line ends vary
I need a reg exp that will parse something like- 2 * 240pin where
Need to parse a Class declaration line in Java using regular expression e.g. String
I need parse through a file and do some processing into it. The file
Hi I need parse and deserialize pseudo JSON string. Input data: {aBubbleData[ 'jaja2581' ]={
Need to parse a file for lines of data that start with this pattern

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.