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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:27:43+00:00 2026-05-10T19:27:43+00:00

I’m looking for a clean C++ way to parse a string containing expressions wrapped

  • 0

I’m looking for a clean C++ way to parse a string containing expressions wrapped in ${} and build a result string from the programmatically evaluated expressions.

Example: ‘Hi ${user} from ${host}’ will be evaluated to ‘Hi foo from bar’ if I implement the program to let ‘user’ evaluate to ‘foo’, etc.

The current approach I’m thinking of consists of a state machine that eats one character at a time from the string and evaluates the expression after reaching ‘}’. Any hints or other suggestions?

Note: boost:: is most welcome! 🙂

Update Thanks for the first three suggestions! Unfortunately I made the example too simple! I need to be able examine the contents within ${} so it’s not a simple search and replace. Maybe it will say ${uppercase:foo} and then I have to use ‘foo’ as a key in a hashmap and then convert it to uppercase, but I tried to avoid the inner details of ${} when writing the original question above… 🙂

  • 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. 2026-05-10T19:27:44+00:00Added an answer on May 10, 2026 at 7:27 pm
    #include <iostream> #include <conio.h> #include <string> #include <map>  using namespace std;  struct Token {     enum E     {         Replace,         Literal,         Eos     }; };  class ParseExp { private:     enum State     {         State_Begin,         State_Literal,         State_StartRep,         State_RepWord,         State_EndRep     };      string          m_str;     int             m_char;     unsigned int    m_length;     string          m_lexme;     Token::E        m_token;     State           m_state;  public:     void Parse(const string& str)     {         m_char = 0;         m_str = str;         m_length = str.size();     }      Token::E NextToken()     {         if (m_char >= m_length)             m_token = Token::Eos;          m_lexme = '';         m_state = State_Begin;         bool stop = false;         while (m_char <= m_length && !stop)         {             char ch = m_str[m_char++];             switch (m_state)             {             case State_Begin:                 if (ch == '$')                 {                     m_state = State_StartRep;                     m_token = Token::Replace;                     continue;                 }                 else                 {                     m_state = State_Literal;                     m_token = Token::Literal;                 }                 break;              case State_StartRep:                 if (ch == '{')                 {                     m_state = State_RepWord;                     continue;                 }                 else                     continue;                 break;              case State_RepWord:                 if (ch == '}')                 {                     stop = true;                     continue;                 }                 break;              case State_Literal:                 if (ch == '$')                 {                     stop = true;                     m_char--;                     continue;                 }             }              m_lexme += ch;         }          return  m_token;     }      const string& Lexme() const     {         return m_lexme;     }      Token::E Token() const     {         return m_token;     } };  string DoReplace(const string& str, const map<string, string>& dict) {     ParseExp exp;     exp.Parse(str);     string ret = '';     while (exp.NextToken() != Token::Eos)     {         if (exp.Token() == Token::Literal)             ret += exp.Lexme();         else         {             map<string, string>::const_iterator iter = dict.find(exp.Lexme());             if (iter != dict.end())                 ret += (*iter).second;             else                 ret += 'undefined(' + exp.Lexme() + ')';         }     }     return ret; }  int main() {     map<string, string> words;     words['hello'] = 'hey';     words['test'] = 'bla';     cout << DoReplace('${hello} world ${test} ${undef}', words);     _getch(); } 

    I will be happy to explain anything about this code 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 188k
  • Answers 188k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For starters, XSLT must always be valid XML. Yours clearly… May 12, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer This probably isn't fast enough for your needs (which sound… May 12, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer I'd probably just save the managedObjectContext and reopen the persistent… May 12, 2026 at 5:38 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.