简体   繁体   English

python中的正则表达式,替换单词中除#hashtag和@username之外的所有元音

[英]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.

相关问题 用于替换所有下划线的正则表达式,除非它们是主题标签的一部分 - Regex for replacing all underlines except if they are part of a hashtag Python 3 正则表达式:删除所有标点符号,特殊单词模式除外 - Python 3 Regex: remove all punctuation, except special word pattern Python 正则表达式匹配关键字的所有变体,除非前面有大写单词 - Python Regex to match a all variations of a keyword except if preceded by a capitalized word 使用正则表达式查找字符串中除单词中的所有单词以外的所有单词 - Find all the words in string except one word in python with regex 删除元音,除非它是单词的开头 - Remove vowels except if it the starting of the word Python-用正则表达式中的字符串替换字符串EXCEPT的所有实例 - Python - Replace all instances of string EXCEPT for those in regex Python正则表达式替换所有模式,除非它紧挨着重复模式 - Python regex replace all patterns except when it is next to a repeated pattern Python正则表达式 - 替换除大括号之外的所有字符 - Python regex - Replace all characters except those between braces Python Regex 匹配除以主题标签开头的单词之外的所有内容 - Python Regex match everything except words that start with a hashtag 如何在python中用下划线替换单词的元音? - How can I replace the vowels of a word with underscores in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM