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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:56:26+00:00 2026-06-12T11:56:26+00:00

To summarize: How can I prevent my regex pattern from mistaking segments of strings

  • 0

To summarize: How can I prevent my regex pattern from mistaking segments of strings as being a whole-word variable name? It is replacing letters that are part of a bigger word even though I use word boundaries \b.

What I am trying to do: I am working on a calculator. It has a list of variables, and before passing the expression to the parser I call my function ParseVars() to do a regex_search using the pattern for variable matching. Once it has all the tokens that match my variable pattern, I check to see if that string is in fact in the list of variable names, if so, I replace the string with the variables value. Also, every time a calculation is made in the parser, I define a constant with the name ans1, ans2, and so on.

The problem is: Let’s say I have a variable defined, named a, and its value is 6. (By the way I keep track of these in a map<string,double> Vars; When I do ParseVars("ans1") the resulting string is "ans1". Also with ParseVar(), the string ans1+ans2+9 stays the same. The string 9+a becomes 9+6. So, so far my regex works as expected.

BUT, if I do ParseVars("ans1+a"), the resulting string is "6ns1+6". I am confused as to why the word boundaries on my regular expression is only failing if I use the variable, ‘a’ can always be found in ‘ans1’, but it only gets replaced if ‘a’ is alone somewhere else in the string.

What I have: Here is my regex pattern: \b([a-z][a-z0-9_]*)\b Shouldn’t this only match whole words? The word boundary works fine until ‘a’ is alone elsewhere in the string. Maybe it’s my ParseVars() function, here is the code:

map<string,double> Vars;

// Variables must be a whole word, start with a letter, and
// optionally have other letters, numbers, and underscores.
sregex VarPattern = sregex::compile("\\b([a-z][a-z0-9_]*)\\b");

string Calculator::ParseVars(string expr) {
    if (Vars.empty()) return expr;

    string newExpr = StrToLower(expr);
    const sregex_iterator End;

    // Loop through all possible variable matches
    for (sregex_iterator i(expr.begin(), expr.end(), VarPattern); i != End; ++i) {
        string name = (*i)[0];

        // If it is a variable
        if (Vars.find(name) != Vars.end()) {
            int rPos = 0;

            // Replace all occurrences of it
            while ((rPos = newExpr.find(name, rPos)) != string::npos) {
                newExpr.replace(
                    rPos, name.length(),
                    lexical_cast<string,double>(Vars[name])
                );
            }
        }
    }

    return newExpr;
}

With a being equal to 6, how can I prevent ans1+a from becoming 6ns1+6 instead of the desired ans1+6?

  • 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-06-12T11:56:28+00:00Added an answer on June 12, 2026 at 11:56 am

    Well, I found the solution. I’m putting my answer here for anybody who has run across a similar problem.

    The problem was I was using a basic string replace AFTER the regex had matched, so the word boundaries worked, it is just that the string replace function was replacing every occurrence of the string regardless of word boundaries. I have to use regex_replace(), here is what I ended up with:

    map<string,double> Vars;
    
    // Variables must be a whole word, start with a letter, and
    // optionally have other letters, numbers, and underscores.
    sregex VarPattern = sregex::compile("\\b([a-z][a-z0-9_]*)\\b");
    
    string Calculator::ParseVars(string expr) {
        if (Vars.empty()) return expr;
        string newExpr = StrToLower(expr);
        const sregex_iterator End;
    
        // Loop through all possible variable matches
        for (sregex_iterator i(expr.begin(), expr.end(), VarPattern); i != End; ++i) {
            string name = (*i)[0];
    
            // If it is a variable
            if (Vars.find(name) != Vars.end()) {
                sregex rgxName = sregex::compile("\\b" + name + "\\b");
    
                // Replace all occurrences of it
                newExpr = xpressive::regex_replace(
                    newExpr, rgxName,
                    lexical_cast<string,double>(Vars[name])
                );
            }
        }
    
        return newExpr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a problem that I can summarize as it follows: I have a
Can anyone summarize or bulleted list the types of things that go in each
Can someone please summarize the advantages of creating an Azure WokerRole vs. simply starting
Can anyone summarize the relationship between the following items? Content View View Controller Nib
Can anyone summarize what is the correct usage of realloc() ? What do you
Does anyone know of any tools out there which can summarize changes to Java
I'm developing an application in java which can take textual information from different web
Using PHP's proc_open() , I can start a process, read from STDOUT and STDERR
Can anyone please summarize, what exactly features gives you adding PowerMock on top of
To summarize a whole lot and describe the situation, I have a form with

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.