Answer the question
In order to leave comments, you need to log in
How to change an arbitrary parameter after a specific word in a string?
I have a string
text = 'background:red;border-color:rgba(255,255,255,0);color:#0ff;'
print(re.findall(r'\w+', text))
for x in self.rad.styleSheet().split(';') :
valx = x.split(':')
#print(valx)
X_1 = valx[0]
try:
X_2 = valx[1]
except:
X_2 = None
if valx[0] == "color" :
X_2 = "blue"
if not X_2 == None :
string = string + X_1 + ":" + X_2 + ";"
print(string)
Answer the question
In order to leave comments, you need to log in
If the string template is always the same, without exceptions, then it is possible without regular expressions:
In [1]: text = 'background:red;border-color:rgba(255,255,255,0);color:#0ff;'
In [2]: text = text[:text.find(';color:')] + '#FFFFF;'
In [3]: text
Out[3]: 'background:red;border-color:rgba(255,255,255,0)#FFFFF;'
In [1]: import re
In [2]: text = 'background:red;border-color:rgba(255,255,255,0);color:#0ff;'
In [3]: to_replace = '#FFFFF'
In [4]: text = text.replace(re.search(r'(^|[^-])color:(.*?)\;', text).group(2), to_replace)
In [5]: text
Out[5]: 'background:red;border-color:rgba(255,255,255,0);color:#FFFFF;'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question