如何使用正则表达式列出以元音开头的单词

2024-09-27 00:22:03 发布

您现在位置:Python中文网/ 问答频道 /正文

那么,我有一个句子如下:

sent = "My name is xyz and I got my name from my parents. My email address is nomail@gmail.com"

我想得到这个句子中所有以元音开头的单词,比如我是。到目前为止,这是我的正则表达式,它不起作用。你知道吗

re.findall('^(aeiou|AEIOU)[\w|\s].',sent)

这就是我得到的结果

['. ', '..', '.s', '@g', '.c']

任何帮助都将不胜感激。你知道吗


Tags: andnamefromisaddressemailmygmail
2条回答

首先,括号不平衡,没有检查单词边界。试试这个:

"\b[(aeiou|AEIOU)].*?\b"

可以将re.findallre.I一起使用:

import re
sent = "My name is xyz and I got my name from my parents. My email address is nomail@gmail.com"
result = re.findall('(?<=\W)[aeiou]\w+|(?<=\W)[aeiou]', sent, re.I)

输出:

['is', 'and', 'I', 'email', 'address', 'is']

相关问题 更多 >

    热门问题