python re.sub - alternative replacement patterns

后端 未结 2 1821
自闭症患者
自闭症患者 2020-12-04 02:48

I want to provide alternative replacement patterns to re.sub.

Let\'s say i\'ve got two search patterns as alternatives, like this:

re.sub(r\"[A-Z]+|[         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 03:42

    Usually, you would just use two replacements:

    re.sub(r"[A-Z]+", replacement1, string)
    re.sub(r"[a-z]+", replacement2, string)
    

    Anticlimactic, right?

    It's actually less code than the alternatives usually, and it's far clearer what you're doing.

提交回复
热议问题