如何使用正则表达式用空格替换字符之间的短划线

前端之家收集整理的这篇文章主要介绍了如何使用正则表达式用空格替换字符之间的短划线前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用正则表达式替换出现在带空格的字母之间的短划线.例如,用ab cd替换ab-cd

以下匹配字符 – 字符序列,但也替换字符[即ab-cd导致d,而不是ab cd,因为我希望]

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

我如何适应以上只能取代 – 部分?

您需要捕获 – 组之前和之后的字符并将其用于替换,即:
import re
subject = "ab-cd"
subject = re.sub(r"([a-z])\-([a-z])",r"\1 \2",subject,re.IGNORECASE)
print subject
#ab cd

DEMO

http://ideone.com/LAYQWT

REGEX EXPLANATION

([A-z])\-([A-z])

Match the regex below and capture its match into backreference number 1 «([A-z])»
   Match a single character in the range between “A” and “z” «[A-z]»
Match the character “-” literally «\-»
Match the regex below and capture its match into backreference number 2 «([A-z])»
   Match a single character in the range between “A” and “z” «[A-z]»

\1 \2

Insert the text that was last matched by capturing group number 1 «\1»
Insert the character “ ” literally « »
Insert the text that was last matched by capturing group number 2 «\2»

猜你在找的正则表达式相关文章