Anyone good at Python...

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:
Code:
string = 'this is a test string'
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:
Code:
variable = 1.0 * 2.0 + 3.0 * 4.0 ** 5.0
where I want to replace the operand '*' with another like operand, such as '-'. Searching for '*' and replacing it with '-' produces the following:
Code:
variable = 1.0 - 2.0 + 3.0 - 4.0 -- 5.0
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:
Code:
variable = 1.0 + sin(2.0)
I might want to replace sin() with cos(). Fine, unless I have:
Code:
variable = 1.0 + sinh(2.0)
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
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.
 
Regular expressions can be a bitch. Have you read the standard HOWTO?

Edit: You might find a solution in the (unofficial) 'Python cookbok'. Personally I'd use Perl for such tasks.
 
Last edited by a moderator:
I barely know Python, much less any other language. This is my first "real" attempt at writing useful code. I've looked through the howto, but I'll do some more digging.

I did a search on the cookbook but didn't have enough time the other night to do an exhaustive one... I'll try that again too. Hopefully there is something in there.

I think I have solved my problem with recognizing substrings of "strings of interest" - if I expand the search string to include whitespace and/or parentheses, I can guarantee only exact matches. So I'll use ' + ' and ' / ' and ' ** ' etc., and for functions I think ' sinh(' and ' asin(' etc. should work.

Now I just have to figure out how to access one match from the middle of a string having multiple matches.
 
Bigus Dickus said:
Now I just have to figure out how to access one match from the middle of a string having multiple matches.
I'm by no means very good at Python myself. I've used it mostly just for the ease of wxPython.

Anyway: There is a recipie for modifying the Nth match of a rexexp in the 'cookbook'. As for other languages; the regular expressions in Python are modeled from Perl, but they're missing some functionality that makes Perl better for string manipulation. It's harder in Python to get access to subpattern matches, and I think it's still lacking the operator that allow you to automagically continue matching from the point of the last match which is very handy.

If you're dealing with structured tasks maybe some of the parsing tools for python could help. Good luck.

Edit: Removed a double negative that made no sense.
 
Last edited by a moderator:
Back
Top