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

kyr*_*nia 6 python regex

我想用正则表达式替换出现在带空格的字母之间的短划线.例如替换ab-cdab cd

以下匹配字符 - 字符序列,但也替换字符[即ab-cd结果a d,而不是ab cd我想要的]

 new_term = re.sub(r"[A-z]\-[A-z]", " ", original_term)
Run Code Online (Sandbox Code Playgroud)

我如何调整以上只能更换-零件?

Tig*_*kT3 6

使用对捕获组的引用:

>>> original_term = 'ab-cd'
>>> re.sub(r"([A-z])\-([A-z])", r"\1 \2", original_term)
'ab cd'
Run Code Online (Sandbox Code Playgroud)

当然,这假定您original_term.replace('-', ' ')无论出于何种原因都不能这样做.也许你的文本使用连字符,它应该使用短划线或其他东西.


Ped*_*ito 6

您需要捕捉的人物之前之后-一组,并将其用于替换,即:

import re
subject = "ab-cd"
subject = re.sub(r"([a-z])\-([a-z])", r"\1 \2", subject , 0, re.IGNORECASE)
print subject
#ab cd
Run Code Online (Sandbox Code Playgroud)

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»
Run Code Online (Sandbox Code Playgroud)