如何使用正则表达式用空格替换字符之间的短划线
发布时间:2020-12-14 06:22:15 所属栏目:百科 来源:网络整理
导读:我想用正则表达式替换出现在带空格的字母之间的短划线.例如,用ab cd替换ab-cd 以下匹配字符 – 字符序列,但也替换字符[即ab-cd导致d,而不是ab cd,因为我希望] new_term = re.sub(r"[A-z]-[A-z]"," ",original_term) 我如何适应以上只能取代 – 部分? 您需
我想用正则表达式替换出现在带空格的字母之间的短划线.例如,用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? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |