Reputation: 1
S1C(SCC1)C1=COC2C(C{OC}C3C(OC=C3)C2)C1=O
In the above string, I want the program to ignore {OC} or technically anything in between these flower brackets but work normally with rest of the string. I have a file which thousands of such strings. Some strings have more than one set of flower brackets. How should it be done?
Presently I use python 2.5
version.
Upvotes: 0
Views: 805
Reputation: 5412
You can iterate over the string and keep track of characters that are not in between brackets. The following code assumes no '{' character inside the string
string = "S1C(SCC1)C1=COC2C(C{OC}C3C(OC=C3)C2)C1=O"
output = ""
brace_found = False
for i in range(len(string)):
if brace_found:
if string[i] == "}":
brace_found = False
else:
if string[i] != "{":
output+=string[i]
else:
brace_found = True
print output
# S1C(SCC1)C1=COC2C(CC3C(OC=C3)C2)C1=O
Upvotes: 0
Reputation: 82755
This might help. Using regex.
import re
s = "S1C(SCC1)C1=COC2C(C{OC}C3C(OC=C3)C2)C1=O"
print re.sub("\{(.*?)\}", " ", s) #Replacing curly brackets and its content by space.
Output:
S1C(SCC1)C1=COC2C(C C3C(OC=C3)C2)C1=O
Upvotes: 2
Reputation: 620
You can use string slicing for this.
Note - This will work correctly only if you have one such bracket in string
str = "S1C(SCC1)C1=COC2C(C{OC}C3C(OC=C3)C2)C1=O"
startofbracket = str.find("{")
endofbracket = str.find("}")
print str[:startofbracket]+str[endofbracket+1:]
Upvotes: 1