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 6908839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:36:47+00:00 2026-05-27T08:36:47+00:00

I’m trying to take a string and then retrieve the required elements from it.

  • 0

I’m trying to take a string and then retrieve the required elements from it. At the moment, I can’t seem to find a way of doing it because the input string that will change from time to time, although the elements won’t.

My code so far is below. What I want to do is extract the '.V/' from the string when parsed.

My code so far below will work, but I need it to be more generic as there are many elements within the inputted string

  • i.e

    .V/ER/12/FRG/45S/16JAN
    .E/45/SHAM/CAMP/2 
    

    and I would need to retrieve .V/ and .E/

 

std::vector<std::string>elements;
std::string input = ".V/ER/12/FRG/45S/16JAN ";

bool result = qi::parse(input.begin(),input.end(),
        *(*(qi::char_ - " /ER/12/FRG/45S/16JAN\n") >> " /ER/12/FRG/45S/16JAN\n"),
        elements
        );  
  • 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-27T08:36:48+00:00Added an answer on May 27, 2026 at 8:36 am

    I’d really suggest using a regular expression (boost regex or std::regex) for this job.

    The regular expression would probably look like

    std::regex re("^(\\.[EV]/).*?$"); // the first submatch is the part you are looking for
    

    Here is a bit of spirit in case you really need it:

    #include <boost/spirit/include/qi.hpp>
    namespace qi = boost::spirit::qi;
    
    typedef std::string::const_iterator It;
    
    int main()
    {
        std::vector<std::string>elements;
        std::string input = 
            ".V/ER/12/FRG/45S/16JAN\n"
            ".E/45/SHAM/CAMP/2";
    
        It first(input.begin()), last(input.end());
    
        bool ok = qi::parse(first, last,
                (
                  '.' > qi::char_("EV") > '/' 
                     >> qi::omit [ *(qi::char_ - qi::eol) ] 
                ) % qi::eol,
                elements);
    
        if (ok)
        {
            for (int i=0; i<elements.size(); ++i)
                std::cout << elements[i] << std::endl;
        } else
        {
            std::cerr << "Parse failed at '" << std::string(first, last) << std::endl;
        }
    
    }
    

    This will output

    V
    E
    

    If you want to show ‘.E/’ there, there are many ways about it, e.g.

    bool ok = qi::parse(first, last,
            (
              (qi::char_('.') > qi::char_("EV") > qi::char_('/' )
                 >> qi::omit [ *(qi::char_ - qi::eol) ] )
            ) % qi::eol,
            elements);
    

    output:

    .V/
    .E/
    

    Bonus

    To show how to include the ‘tail’ of the line, possibly storing into map:

    #include <map>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/fusion/adapted/std_pair.hpp>
    namespace qi = boost::spirit::qi;
    
    typedef std::string::const_iterator It;
    
    int main()
    {
        typedef std::map<std::string, std::string> result_t;
        // or use a list to allow duplicate keys:
        // typedef std::list<std::pair<std::string, std::string> > result_t;
        result_t mappings;
    
        std::string input = 
            ".V/ER/12/FRG/45S/16JAN\n"
            ".E/45/SHAM/CAMP/2";
    
        It first(input.begin()), last(input.end());
    
        bool ok = qi::parse(first, last, (
                      qi::raw [ '.' > qi::char_("EV") > '/' ]
                    > qi::raw [ *(qi::char_ - qi::eol) ]
                ) % qi::eol,
                mappings);
    
        if (ok)
        {
            for (result_t::const_iterator it=mappings.begin();
                 it!=mappings.end(); ++it)
            {
                std::cout << it->first << " maps to " << it->second << std::endl;
            }
        } else
        {
            std::cerr << "Parse failed at '" << std::string(first, last) << std::endl;
        }
    
    }
    

    would output

    .E/ maps to 45/SHAM/CAMP/2
    .V/ maps to ER/12/FRG/45S/16JAN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.