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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:47:41+00:00 2026-06-07T05:47:41+00:00

Given an equation for eval: eval_str = ‘VAR1 > 0 and VAR1 < 10

  • 0

Given an equation for eval:

    eval_str = 'VAR1 > 0 and VAR1 < 10 and (VAR2 == VAR1::VALUE_X or VAR2 == VAR2::VALUE_X)'

Task: I need to replace variables (VAR1,VAR2 in this example), with their actual values, and also surround the given ‘constant’ (VAR1::VALUE_X) with quotes.

Problem: Since the variable name exists in the constant and in the eval string, and since the variable could replaced with a string that contains the variable name itself – I run into issues where the variable name in the constant value will be replaced by another constant or variable value. Better shown…

     eval_str = '(VAR2 == VAR2::VALUE_X or VAR2 != VAR2::VALUE_Z) and (VAR1 > 0 and VAR1 < 10)'
     var_dict = {'VAR1':'5','VAR2':'VAR1::VALUE_Y'}

     # what I do is strip out most of the special characters
     # the following findall = ['VAR1', '0', 'and', 'VAR1', '10', 'and', 'VAR2', 'VAR1::VALUE_X', 'or', 'VAR2', 'VAR2::VALUE_X']
     for x in re.findall('[^\s()<>=!]+',eval_str):
        # skip digits, and, or, None *need to improve regex
        if x.replace('.','').isdigit() or x == 'and' or x == 'or' or x == 'None':
           continue
        # if variable is in dict
        if x in var_dict.keys():
           # only replace first
           eval_str = eval_str.replace(x,var_dict[x],1) 
        # if is constant
        elif x.__contains__('::'):
           eval_str = eval_str.replace(x,'\'%s\''%x,1)

     print eval_str
     # (5::VALUE_Y == '5::VALUE_Y::VALUE_X' or VAR2 != 'VAR2::VALUE_Z') and (VAR1 > 0 and VAR1 < 10)

Instead of incrementing through each variable/value, perhaps it would be better to replace them all using a regex for each one? Or could I fix my existing solution if there was a way to remember my position in the string after each replace?

TYVM!

  • 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-07T05:47:42+00:00Added an answer on June 7, 2026 at 5:47 am

    To replace VAR1 leaving along VAR1::VALUE_X you could use negative lookahead:

    string = re.sub(r'VAR\d(?!\:\:)', lambda m: var_dict[m.group(0)], string)
    

    A more robust solution would parse the string into an AST and eval it.

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

Sidebar

Related Questions

Given this example, how would I return the result of the equation rather than
Given a point (x1, y1) and an equation for a line (y=mx+c), I need
I need a way of taking an equation given as a string and finding
Given a problem, how to come up with a recurrence equation? For example :
This is the problem I need help with: equation: r = a1*x1 + a2*x2
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;
Standard equation for generating DCT coefficients in JPEG compression process is DCT formula given
Given a 2 dimensional plane in which there are n points. I need to
If we are given with an array of non-linear equation coefficients and some range,
How does one go about drawing an hyperplane (given the equation) in 3D in

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.