I am trying to replace all the word in a give sentence with a random word list. Here is my code:
import re
import random
WORDS = ["Brawk" , "Buh-gok", "Bok bok", "Bawk"] # My random word list
PATTERN = r"([a-zA-Z0-9']+)"
DELIMITER = " "
def callback(matchobj):
return random.choice(WORDS)
def parse_sentence(sentence):
return re.sub(PATTERN, callback, sentence)
When I run the code, this is what happens:
>>> print(parse_sentence("some text's[color=#ff8a00]smoe more text[/color]{n}"))
Bok bok Bok bok[Buh-gok=#Buh-gok]Bok bok Buh-gok Buh-gok[/Bawk]{Brawk}
I need it to be Buh-gok Bok bok[color=#ff8a00]Bok bok Bok bok Bawk[/color]{n}
, so is there anyway to ignore it if it is inside []
or {}
?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…