Python re.sub(): how to substitute all 'u' or 'U's with 'you'

后端 未结 5 1097
暖寄归人
暖寄归人 2021-01-30 03:19

I am doing some text normalization using python and regular expressions. I would like to substitute all \'u\'or \'U\'s with \'you\'. Here is what I have done so far:

         


        
5条回答
  •  礼貌的吻别
    2021-01-30 03:47

    it can also be achieved with below code

    import re
    
    text = 'how are u? umberella u! u. U. U@ U# u '
    print (re.sub (r'[uU] ( [^a-z] )', r' you\1 ', text))
    

    or

    print (re.sub (r'[uU] ( [\s!,.?@#] )', r' you\1 ', text))
    

提交回复
热议问题