我想用正则表达式替换出现在带空格的字母之间的短划线.例如替换ab-cd
为ab cd
以下匹配字符 - 字符序列,但也替换字符[即ab-cd
结果a d
,而不是ab cd
我想要的]
new_term = re.sub(r"[A-z]\-[A-z]", " ", original_term)
Run Code Online (Sandbox Code Playgroud)
我如何调整以上只能更换-
零件?
使用对捕获组的引用:
>>> 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('-', ' ')
无论出于何种原因都不能这样做.也许你的文本使用连字符,它应该使用短划线或其他东西.
您需要捕捉的人物之前和之后的-
一组,并将其用于替换,即:
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
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)