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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:50:22+00:00 2026-06-18T17:50:22+00:00

Given a variable name such as myvariable , what regex could be used to

  • 0

Given a variable name such as myvariable, what regex could be used to replace (refactor) references to it with another variable name myreplacementvariable

e.g.

addr = &myvariable;

should turn into

addr = &myreplacementvariable;

BUT

int myvariable2 = 0;

should be left alone (because it’s a different variable name that happens to contain the name of myvariable)

I am looking for a line or two of Python, probably with Regex.

Note: I am aware parsing C is incredibly difficult and am not looking for anything that thinks about scope. I also forsee char *mystr = "myvariable"; causing problems but I can work around that.

Thanks in advance

  • 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-18T17:50:23+00:00Added an answer on June 18, 2026 at 5:50 pm

    Maybe this helps:

    value = "addr = &myvariable;"
    findVar = "myvariable"
    m = re.findall("[&]{0};".format(findVar), value)
    print m
    # prints: ['&myvariable;']
    

    Note that I escaped the ampersand by putting it within the [].
    You could also escape it with two backslashes:

    m = re.findall("\\&{0};".format(findVar), value)
    

    Edit:

    Here’s a re.sub version based on discussion in the comments:

    value = "addr = &myvariable;\naddr = &myvariable;\nfuncCall( &myvariable )"
    oldVarName = "myvariable"
    newVarName = "mynewvariable"
    m = re.sub("(\\&){0}(;?)".format(oldVarName), r"\1"+newVarName+r"\2", value)
    print m
    # this will print:
    # addr = &mynewvariable;
    # addr = &mynewvariable;
    # funcCall( &mynewvariable )
    

    This pattern will have the same result as:

    value.replace("&"+oldVarName,"&"+newVarName)
    

    I’m using the parentheses for matching the ampersand at the start and the possible semicolon at the end. Then I’m using the \1 and \2 to put these matches back in within the replacement string. Note that this result would be similar to using: value.replace(“&”+oldVarName, “&”+newVarName)

    EDIT:
    This is probably closer to what you need.

    It replaces every instance starting with an ampersand AND contains the whole old variable name and doesn’t contain any of the characters afterwards that are within [A-Za-z0-9_].

    value = "addr = &myvariable;\naddr = &myvariable;\nfuncCall( &myvariable )\nfuncCall2( &myvariable, &myvariablelongername )"
    oldVarName = "myvariable"
    newVarName = "mynewvariable"
    m = re.sub("(\\&){0}(?![A-Za-z0-9_])".format(oldVarName), r"\1"+newVarName, value)
    print m
    # prints:
    # addr = &mynewvariable;
    # addr = &mynewvariable;
    # funcCall( &mynewvariable )
    # funcCall2( &mynewvariable, &myvariablelongername )
    

    (That last part is any valid character for in a variable name in C, after the first character which is required to start with: [A-Za-z_]. This is also mentioned in the answer by ‘nhahtdh’)

    Using what nhahtdh provided as an example this would be a shorter version of the last example:

    re.sub("(\\&){0}(?!\b)".format(oldVarName), r"\1"+newVarName, value)
    

    Since it was new to me when writing this answer and it got mixed up in the comments by myself in this answer I’m adding this as information: The r in front of the strings like r”\1″ turn the string into a raw string.

    r ‘a raw string where \ are kept (literalized): handy for regular expressions and windows paths!’
    Source: http://rgruet.free.fr/PQR26/PQR2.6.html#Strings

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

Sidebar

Related Questions

Given <xsl:variable name=datePrecision as=element()*> <p>Year</p> <p>Month</p> <p>Day</p> <p>Time</p> <p>Timestamp</p> </xsl:variable> The expression $datePrecision[5] returns
I have a C program that prints every environmental variable, whose name is given
I always thought operator is determined if the given variable is of the given
Given this variable in tcsh: set i = ~/foo/bar.c how can I get just
Given a variable which holds a string is there a quick way to cast
Given that I have a variable $peopleSize in the format (I've already extracted the
I need to check a variable vi_theIndex for its value. At the given moment
The compiler, given the following code, tells me Use of unassigned local variable 'x'.
/one_path/some_page.php?var1=value1&var2=value2&var3=value3 Suppose I'm given something like the above. Not just the query variable string,
I have been given some 'reports' from another piece of software that contains data

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.