replace a string not located between two specific words
Given a string, I need to replace a substring with another in an area not located between two given words.
For example:
substring: "ate" replace to "drank", 1st word - "wolf", 2nd word - "chicken"
input: The wolf ate the chicken and ate the rooster
output: The wolf ate the chicken and drank the rooster
Currently, the only solution I have is extremely unclean:
1) Replace the string located between the two words to a temporary substring, via Replace a string located between
2) replace the string I originally wanted
3) revert the temporary string to the original string
Edit:
I specifically asked a slightly different question than my case to keep the answer relevant for future readers.
My specific need is splitting a string according to ":", when I need to disregard ":" that are between "<" and ">" brackets that can be chained, where the only promise is that the number of opening brackets equal the number of closing brackets.
So for example, In the following case:
input a : <<a : b> c> : <a < a < b : b> : b> : b> : a
output [a, <<a : b> c>, <a < a < b : b> : b> : b>, a]
If the answers are very different, I'll start another question.
Use re.sub
one-liner function.
>>> s = "The wolf ate the chicken and ate the rooster"
>>> re.sub(r'wolf.*?chicken|bateb', lambda m: "drank" if m.group()=="ate" else m.group(), s)
'The wolf ate the chicken and drank the rooster'
Update:
Updated problem would be solved by using regex
module.
>>> s = "a : <<a : b> c> : <a < a < b : b> : b> : b> : a"
>>> [i for i in regex.split(r'(<(?:(?R)|[^<>])*>)|s*:s*', s) if i]
['a', '<<a : b> c>', '<a < a < b : b> : b> : b>', 'a']
DEMO
def repl(match):
if match.group()=="ate":
return "drank"
return match.group()
x="The wolf ate the chicken and ate the rooster"
print re.sub(r"(wolf.*chicken)|bateb",repl,x)
你可以使用一个函数进行替换,用re.sub
来完成这个技巧
上一篇: 烧瓶,蓝图使用芹菜任务并获得循环输入
下一篇: 替换不在两个特定单词之间的字符串