Bigus Dickus
Regular
Cause I ain't!
I've been searching, googling, reading, experimenting, even posting on a python help forum and still haven't figured out how to do something that seems pretty simple.
Here's my problem. I want to do a replacement of a character or group of characters within a string object, using either string.replace or re.sub methods (don't care which, so long as it works), or something else if you have other suggestions. The catch is that very often the string will have several of the "original" characters distributed throughout the string, and not only do I not want to replace them all but I also want to choose one from the beginning, end, middle, or perhaps a specific one.
Say the string is something like:
Say the character I want to replace is 's' and I want to change it to 'S'. But say I want to only change the second or third 's' and leave the rest alone. string.replace only let's you start from the beginning and change however many you specify, and so far as I've figured out re.sub acts similarly.
The problem is compounded because sometimes I want to do this on a mathematical expression such as the following:
where I want to replace the operand '*' with another like operand, such as '-'. Searching for '*' and replacing it with '-' produces the following:
So here no only do I replace all of them (which I don't want to do), but I produce the malfunction operand '--' which won't execute properly. In these cases, I've tried to manually recognize when '**' is found, and if so realize that even though '*' was also found I ignore that case (finding '*') and remove it from a possible character to replace. That limits the flexibility of what I can do, though, since in the above example the first two '*' operands will be "hidden" or shielded from accessibility because the string also contains the '**' operand.
Perhaps if I keep a whitespace character on either side of all operands, I can search for ' * ' and it will not be recognized when ' ** ' is present? But that only works for that specific case. I also want to search for and replace functions, such as if I have the string:
I might want to replace sin() with cos(). Fine, unless I have:
In that case, sin will be found, and I can't replace it with any like function because these include tanh, cosh, etc., and I don't want
Any suggestions for any of these problems? My understanding of the intricate usage of the re.compile pattern methods is limited, so perhaps regex's can be used to help with some of this. I've tried, and failed. I'd like to avoid having to do this with lengthy involved conditional loops that try to catch each unique case and handle it accordingly, but if that's the only way some suggestions along those lines would be welcomed too.
I've been searching, googling, reading, experimenting, even posting on a python help forum and still haven't figured out how to do something that seems pretty simple.
Here's my problem. I want to do a replacement of a character or group of characters within a string object, using either string.replace or re.sub methods (don't care which, so long as it works), or something else if you have other suggestions. The catch is that very often the string will have several of the "original" characters distributed throughout the string, and not only do I not want to replace them all but I also want to choose one from the beginning, end, middle, or perhaps a specific one.
Say the string is something like:
Code:
string = 'this is a test string'
The problem is compounded because sometimes I want to do this on a mathematical expression such as the following:
Code:
variable = 1.0 * 2.0 + 3.0 * 4.0 ** 5.0
Code:
variable = 1.0 - 2.0 + 3.0 - 4.0 -- 5.0
Perhaps if I keep a whitespace character on either side of all operands, I can search for ' * ' and it will not be recognized when ' ** ' is present? But that only works for that specific case. I also want to search for and replace functions, such as if I have the string:
Code:
variable = 1.0 + sin(2.0)
Code:
variable = 1.0 + sinh(2.0)
Code:
variable = 1.0 + tanhh(2.0)
Any suggestions for any of these problems? My understanding of the intricate usage of the re.compile pattern methods is limited, so perhaps regex's can be used to help with some of this. I've tried, and failed. I'd like to avoid having to do this with lengthy involved conditional loops that try to catch each unique case and handle it accordingly, but if that's the only way some suggestions along those lines would be welcomed too.