Search code examples
pythonpython-re

Regular expression for removing text with parenthesis


Take this text for example 泣(な)き出(だ)した, I want to remove the furigana programmatically (ie. 泣き出した). I tried

re.sub(r"\([^()]*\)", "", '泣(な)き出(だ)した')

but it returns 泣した instead. What is the better regular expression?


Solution

  • You should try re.sub(r"\(.*?\)", "", '泣(な)き出(だ)した'), which uses the non-greedy quantifier *?.