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

后端 未结 5 1099
暖寄归人
暖寄归人 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:32

    Use a special character \b, which matches empty string at the beginning or at the end of a word:

    print re.sub(r'\b[uU]\b', 'you', text)
    

    spaces are not a reliable solution because there are also plenty of other punctuation marks, so an abstract character \b was invented to indicate a word's beginning or end.

提交回复
热议问题