I have a string in Python that is comprised of code in C/C++ syntax. I would like to use regular expressions to search through this string and replace an expression of the form 'pow(a,b)' with 'a^b'

I've attempted to do this, but my code does not seem to be working. What would I have to change to be able to replace the expression?

import re

def main():
    expr = '5*pow(a,b) + 3*pow(b,c) + 3*a + 4*b'
    print re.sub('pow(.,.)' , '.^.', expr)

if __name__ == "__main__":
    main()

The output of this example program is

5*pow(a,b) + 3*pow(b,c) + 3*a + 4*b

but I would like the output to be

5*a^b + 3*b^c + 3*a + 4*b

Escape the open and close parentheses that are meant as literal characters and enclose the data that should be captured in parentheses to create two capture groups. These two capture groups are referenced in your replacement string as \1 and \2.

#!/usr/bin/env python
import re

def main():
    expr = '5*pow(a,b) + 3*pow(b,c) + 3*a + 4*b'
    print re.sub(r'pow\((.),(.)\)' , r'\1^\2', expr)

if __name__ == "__main__":
    main()
commented: Great solution; thank you very much +1

Thank you very much, d5e5; this works extremely well!

For more advanced expressions, I've found that the dot syntax (.) does not seem to work for me:

import re

def main():

    expr = 'p_neg1_1_n/pow(deltax,4) - 4*p_0_1_n/pow(deltax,4)'
    print re.sub(r'pow\((.),(.)\)' , r'\1^\2', expr)

if __name__ == "__main__":
    main()

The output is as follows:

p_neg1_1_n/pow(deltax,4) - 4*p_0_1_n/pow(deltax,4)

What would I have to use in lieu of the dot syntax to be able to do the replacement? I believe that the dot syntax will only operate on characters.

One dot represents one character. Since now you want to match 'deltax', which contains several characters, you can add the plus sign to mean 'one or more characters'. But that matches too much. You want to match only 'deltex', not 'deltax,4) - 4*p_0_1_n/pow(deltax,4)'.

Let's use (\w+) instead of (.+). \w represents only alphanumeric characters, so it will stop matching at the comma, which is what you want.

import re

def main():

    expr = 'p_neg1_1_n/pow(deltax,4) - 4*p_0_1_n/pow(deltax,4)'
    print re.sub(r'pow\((\w+),(\w+)\)' , r'\1^\2', expr)

if __name__ == "__main__":
    main()
commented: Excellent response; again, thank you! +1

Once again, thank you very much, d5e5; this is greatly appreciated, and it works very well for me.

I'm using Python to post-process the output of a program that creates strings of C code. The plot thickens, since the program can also create the following types of (more complicated) expressions:

expr0 = 'p_neg1_1_n/pow(deltax,-4) - 4*p_0_1_n/pow(deltax,4)'    
    expr1 = 'p_neg1_1_n/pow(deltax,(-4)) - 4*p_0_1_n/pow(deltax,(4))'

In these expressions, the number can be -4, or the number can occur in brackets (-4), or (4). I believe that this is valid C-code syntax.

Using regular expressions, is there a way to deal with expr0 and expr1 so that pow(deltax,(-4)) becomes deltax^-4 and pow(deltax,(4)) becomes deltax^4 ?

I'm using Python to post-process the output of a program that creates strings of C code. The plot thickens, since the program can also create the following types of (more complicated) expressions:

expr0 = 'p_neg1_1_n/pow(deltax,-4) - 4*p_0_1_n/pow(deltax,4)'    
    expr1 = 'p_neg1_1_n/pow(deltax,(-4)) - 4*p_0_1_n/pow(deltax,(4))'

In these expressions, the number can be -4, or the number can occur in brackets (-4), or (4). I believe that this is valid C-code syntax.

Using regular expressions, is there a way to deal with expr0 and expr1 so that pow(deltax,(-4)) becomes deltax^-4 and pow(deltax,(4)) becomes deltax^4 ?

I don't know. It seems like a tough one. I think it may require applying a substitution to an expression and then applying another substitution to the resulting string? If I think of a solution later I'll let you know.

Sure, that sounds good d5e5; thank you very much. If there is a way to do this it will certainly be very interesting.

Try this. I didn't have much time for testing but it seems OK to me.

import re

def main():
    pattern = r'pow\((\w+),\(?(-?\w+)\)?\)'
    expr0 = 'p_neg1_1_n/pow(deltax,-4) - 4*p_0_1_n/pow(deltax,4)'    
    expr1 = 'p_neg1_1_n/pow(deltax,(-4)) - 4*p_0_1_n/pow(deltax,(4))'
    
    print re.sub(pattern , r'\1^\2', expr0)
    print re.sub(pattern , r'\1^\2', expr1)

if __name__ == "__main__":
    main()
commented: Wonderful; thank you so much +1

Thank you very much, d5e5! This is greatly appreciated. Once again, thank you!

Is this Python code? a^b is *not* pow(a,b) - you want a**b.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.