假设我会要求用户输入一个字符串。如何在每个以“a”、“e”或“o”结尾的单词末尾添加一个字符?
例如,来自
f = "O, awesome people, help me if ya will"
我会
f = "Oh, awesomeh peopleh, help meh if yah will"
请您参考如下方法:
使用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'
