使用python字符串中的re.findall来查找以大写字母开头的项目?

发布于 2025-02-09 12:17:03 字数 362 浏览 1 评论 0原文

如果我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惟欲睡 2025-02-16 12:17:03

您不需要re:只需使用str.isupper

[i for i in my_string if i[:1].isupper()]

输出:

['Cupcake', 'Dinosaur']

You don't need re: just use str.isupper

[i for i in my_string if i[:1].isupper()]

Output:

['Cupcake', 'Dinosaur']
很酷又爱笑 2025-02-16 12:17:03

那不是字符串。那是字符串清单。您需要类似的东西:

newlst = [k for k in my_string if 'A' <= k[0] <= 'Z']

That's not a string. That's a list of strings. You'll need something like:

newlst = [k for k in my_string if 'A' <= k[0] <= 'Z']
哑剧 2025-02-16 12:17:03
import re

my_string = ['a17b', 'Cupcake', '8ikl3', 'Dinosaur']
pattern = re.compile(r'^[A-Z]')
new_string = [i for i in my_string if pattern.match(i)] 
print(new_string)

输出:

['蛋糕','恐龙']

import re

my_string = ['a17b', 'Cupcake', '8ikl3', 'Dinosaur']
pattern = re.compile(r'^[A-Z]')
new_string = [i for i in my_string if pattern.match(i)] 
print(new_string)

Output:

['Cupcake', 'Dinosaur']

雨的味道风的声音 2025-02-16 12:17:03

为什么它不起作用:

  • findall()搜索一个字符串,但是您在字符串列表中传递了
  • 表达式[^az]将返回 的字符不是 在AZ范围内
  • 您要复制整个单词,但是您只要求第一个字母

修复:

#join the list of strings to get yourself a string that will work with findall()

my_string = ['a17b', 'Cupcake', '8ikl3', 'Dinosaur']
my_string = ' '.join(my_string)

new_string = ['Cupcake', 'Dinosaur']

expression = r'[A-Z][\w]*'
#now re will find all words with capitalized first letters
new_string = re.findall(expression, my_string)

new_string


解码表达式('[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
  • The expression [^A-Z] will return characters that are not in the range A-Z
  • You want to copy the entire word, but you're only asking for the first letter

How to fix:

#join the list of strings to get yourself a string that will work with findall()

my_string = ['a17b', 'Cupcake', '8ikl3', 'Dinosaur']
my_string = ' '.join(my_string)

new_string = ['Cupcake', 'Dinosaur']

expression = r'[A-Z][\w]*'
#now re will find all words with capitalized first letters
new_string = re.findall(expression, my_string)

new_string


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 term

One resource worth checking out is https://regex101.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文