How to replace dash between characters with space using regex

后端 未结 4 1110
猫巷女王i
猫巷女王i 2021-01-18 11:56

I want to replace dashes which appear between letters with a space using regex. For example to replace ab-cd with ab cd

The following matc

4条回答
  •  感情败类
    2021-01-18 12:45

    re.sub() always replaces the whole matched sequence with the replacement.

    A solution to only replace the dash are lookahead and lookbehind assertions. They don't count to the matched sequence.

    new_term = re.sub(r"(?<=[A-z])\-(?=[A-z])", " ", original_term)
    

    The syntax is explained in the Python documentation for the re module.

提交回复
热议问题