说我会要求用户输入一个字符串。如何在以“a”,“e”或“o”结尾的每个单词的末尾添加一个字符?
例如,来自
f = "O, awesome people, help me if ya will"
我有
f = "Oh, awesomeh peopleh, help meh if yah will"
答案 0 :(得分:5)
使用re.sub
re.sub(r'(?i)([aeo])\b', r'\1h', s)
(?i)
有助于进行不区分大小写的匹配。([aeo])
只有在后跟字边界时才会捕获a,e,o。示例:强>
>>> import re
>>> f = "O, awesome people, help me if ya will"
>>> re.sub(r'(?i)([aeo])\b', r'\1h', f)
'Oh, awesomeh peopleh, help meh if yah will'