使用python字符串中的re.findall来查找以大写字母开头的项目?
如果我有一个python字符串,我该如何使用re.findall仅提取以大写字母开头的项目?
我的字符串看起来像这样:
my_string = ['a17b', 'Cupcake', '8ikl3', 'Dinosaur']
我希望我的提取的字符串看起来像:
new_string = ['Cupcake', 'Dinosaur']
这是我的代码到目前为止(不正确):
import re
new_string = re.findall(r'[^A-Z]', my_string)
我要在哪里出错?谢谢。
If I have a Python string, how can I use re.findall to extract only the items that start with a capital letter?
My string looks like this:
my_string = ['a17b', 'Cupcake', '8ikl3', 'Dinosaur']
I want my extracted string to look like this:
new_string = ['Cupcake', 'Dinosaur']
Here's my code so far (not correct):
import re
new_string = re.findall(r'[^A-Z]', my_string)
Where am I going wrong? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要
re
:只需使用str.isupper
输出:
You don't need
re
: just usestr.isupper
Output:
那不是字符串。那是字符串清单。您需要类似的东西:
That's not a string. That's a list of strings. You'll need something like:
输出:
Output:
为什么它不起作用:
findall()
搜索一个字符串,但是您在字符串列表中传递了[^az]
将返回 的字符不是 在AZ范围内修复:
解码表达式('[az] \ w*')
[az]
查找大写字母\ w
查找字符\*
告诉RE告诉RE查找上一项或多个上一项值得检查的资源值得检查OUT是 https://regex101.com
Why it doesn't work:
findall()
searches a string, but you passed in a list of strings[^A-Z]
will return characters that are not in the range A-ZHow to fix:
Decoding Expression ('[A-Z]\w*')
[A-Z]
finds Capitalized letters\w
finds word characters\*
tells re to look for 0 or more of the preceding termOne resource worth checking out is https://regex101.com