A regular replacement is performed a plurality of different

Recently found that there are a lot of content @ ***, where *** is the user's nickname, meaning there is no need to clear out, here used the regular replacement in the information processing twitter reply text processing time can be on the same time matching content is replaced by the following

import re 
text = "Literally y’all bruh. @samanthag_15 @MadalynWiese @FaithannKing @sylebrity "
pattern = re.compile('@.*? ')
res = re.sub(pattern, "", text)
print(res)

result:

Literally y’all bruh.

Observant students will find that there is no space if the last word, the last of which @sylebrity and will not be deleted, lazy me directly in the final plus a space, we do not know how to solve it

Guess you like

Origin blog.csdn.net/a857553315/article/details/93775017