正则表达式删除两个单词之间的所有连字符

I am cleaning a text and I would like to remove all the hyphens and special characters. Except for the hyphens between two words such as: tic-tacs, popcorn-flavoured.

我写了下面的正则表达式,但它删除了每个连字符。

text='popcorn-flavoured---'
new_text=re.sub(r'[^a-zA-Z0-9]+', '',text)
new_text 

我希望输出为:

popcorn-flavoured

评论
fizzuas
fizzuas

尝试这个。

# replace any occurrences where 
# more than ONE hyphen is present
# with an empty character
re.sub('-+', '', text)
点赞
评论