Python regex expression to remove hyphens between lowercase characters

后端 未结 3 1229
感动是毒
感动是毒 2021-01-29 09:24

I need to remove the hyphens between lowercase letters only. This is the expression that I have currently:

re.sub(\'\\[a-z]-\\[a-z]\', \"\", \'hyphen-ated Asia-P         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 10:10

    re.sub(r'([a-z])-([a-z])', r'\1\2', "hyphen-ated Asia-Pacific 11-12")
    

    Captures the letters before and after the hyphen and preserves them when stripping the hyphen. \1 and \2 denotes the first and second captured group, which are the letters in this case.

    Your current code matches the two letters around the hyphen and removes the whole match. You should preserve the letters when substituting.

提交回复
热议问题