I'm writing a regex to find code like this:
if condition:True:
statements
Where the first colon is instead of ==.
I've come up with this so far: r"(if|elif) .* : .* :\n\t"
But I want to find a way to select the first : and replace it with ==. The thing is, I cant find a way to use regex to substitute : without inadvertantly a: replacing the whole if condition etc with == by writing a
re.sub(r"(if|elif) .* : .* :\n\t","==",text)
or b: Replacing every colon in my script with ==, which will cause an Error like
NameError: Expected a ':', received '=='
So, is there a way to substitute only a bit of the regex with ==, or is there another way to do this that I overlooked??
What should happen
Input:
if 3+4:7:
print("Three plus four is actually seven")
elif 3+6:10:
print("3+4 is not 7, yet 3+6=10")
Console:
Three plus four is actually seven
This is Python 3 by the way.