[英]regex in python, replace all vowels in word except #hashtag and @username
I'm trying to learn regex in python to replace all vowels in word except #hashtag and @username.我正在尝试在 python 中学习正则表达式来替换单词中除 #hashtag 和 @username 之外的所有元音。 but I only know regex for:但我只知道正则表达式:
find vowels: [aiueoAIUEO]
找到元音: [aiueoAIUEO]
find @username and #hashtag: ([@#][\\w_-]+)
找到@username 和#hashtag: ([@#][\\w_-]+)
example:例子:
@username blah blah #blah blah #hashtag @blah blah blah
become:变得:
@username blh blh #blah blh #hashtag @blah blh blh
import re
s = "@username blah blah #blah blah #hashtag @blah blah blah"
#([@#][\w_-]+)
print(re.sub(r'[aiueoAIUEO]', '', s))
You may use您可以使用
import re
s = "@username blah blah #blah blah #hashtag @blah blah blah"
print(re.sub(r'([@#][\w-]+)|[aiueoAIUEO]', r'\1', s))
See the regex demo and the Python demo .请参阅正则表达式演示和Python 演示。
With ([@#][\\w-]+)|[aiueoAIUEO]
, the hashtag/mention gets captured into Group 1, and in that case, \\1
backreference puts it back into the resulting string, else a vowel is matched and removed.使用([@#][\\w-]+)|[aiueoAIUEO]
,主题标签/提及被捕获到组 1 中,在这种情况下, \\1
反向引用将其放回结果字符串中,否则匹配元音并且移除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.