Regular expression for removing text with parenthesis

Viewed 73

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?

2 Answers

The parentheses in your character class are half-width (半角) instead of full-width (全角) parentheses.

Replacing them will fix your problem:

import re

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

# '泣き出した'

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

Related