1

My String is

I liked TAG:{Destiny2,the last Destiny game}, now I play TAG{Fortnite,Fortnite} is Destiny

So I don't want to replace "Destiny" from

TAG:{Destiny2,the last Destiny game}     

but I want to replace last word "Destiny" with

TAG:{Destiny:Destiny}    

I always want to ignore string in TAG while replacing.

Expected Output:

I liked TAG:{Destiny2,the last Destiny game}, now I play TAG{Fortnite,Fortnite} is TAG:{Destiny:Destiny}

Please help.

7
  • 1
    What is the error with your code? Or have you not tried anything as of yet? Commented May 3, 2018 at 0:49
  • 1
    So your desired output would be I liked TAG:{Destiny2,the last Destiny game}, now I play TAG{Fortnite,Fortnite} is TAG:{Destiny:Destiny}? Describing your output is generally less helpful than actually providing it (when small enough to provide); please read about writing minimal reproducible examples. Commented May 3, 2018 at 0:50
  • all things I tried either replace all Destiny strings even in the last Destiny game or none Commented May 3, 2018 at 0:51
  • You also need to be explicit about your inputs. If it's just the one string, then replace the last 7 characters and done. But if it's more general, you may need regex or more intelligent parsing Commented May 3, 2018 at 0:51
  • seems like you need a regular expression that overlooks whats between tag{} Commented May 3, 2018 at 0:53

2 Answers 2

2

You need to parse your string first to find out which Destiny substrings are inside a tag, and which aren't. I have done this below with re.split.

My use of re.split returns a list of substrings surrounding the regex pattern TAG:?{.*?}, and because I enclose the pattern in parentheses, the tags are included in the list as well. In this use of re.split, the non-tags will always have an even index, and the tags will always have an odd index. So I check if the index is even, and if so I replace Destiny with TAG:{Destiny,Destiny}.

import re

s = 'TAG:{Destiny2,the last Destiny game}, now I play TAG{Fortnite,Fortnite} is Destiny'
result = []
for i, substring in enumerate(re.split('(TAG:?{.*?})', s)):
    if i % 2 == 0:
        substring = substring.replace('Destiny', 'TAG:{Destiny,Destiny}')
    result.append(substring)
result = ''.join(result)
print(result) # TAG:{Destiny2,the last Destiny game}, now I play TAG{Fortnite,Fortnite} is TAG:{Destiny,Destiny}

This will work as long as you don't have tags nested inside other tags.

Sign up to request clarification or add additional context in comments.

Comments

0
import re

my_string='I liked TAG:{Destiny2,the last Destiny game}, now I play TAG{Fortnite,Fortnite} is Destiny'

match = lambda x: True if len(re.split('(Destiny){1}', x)) == 3 else False 
repl =  lambda x: str.replace(x,'Destiny','TAG:{Destiny:Destiny}') if match(x) else x

l =re.split(r'({.*?})',my_string)
replaced=[repl(i) for i in l]
print(''.join(replaced))

output

I liked TAG:{Destiny2,the last Destiny game}, now I play TAG{Fortnite,Fortnite} is TAG:{Destiny:Destiny}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.