正则表达式不会用点替换连字符

时间:2016-10-13 20:03:42

标签: python regex

以下文字:

comment = """ I took the pill -  I realized only side-effect after I went off it how it affected my eating habits - I put on weight - around 10 lbs - in the 2.5 months on it - no control and syndrome - this was counterproductive !"""

我把正则表达式写成replace hyphen (-) with dot (.)

comment = re.sub (r'(w+\s+)(-)(\s+\w+)', r'\1\. \3 ', comment )

但它不起作用。

我不希望副作用这两个词之间的连字符用点替换。 这就是为什么我不能使用comment.replace ('-', '.')

有任何建议吗?

1 个答案:

答案 0 :(得分:4)

您也可以使用str.replace方法

comment.replace('-', '...')