问题描述
我正在尝试学习如何使用正则表达式,但有一个问题.假设我有字符串
line = 'Cow Apple think Woof`
我想看看 line
是否至少有两个以大写字母开头的单词(当然有).在 Python 中,我尝试执行以下操作
导入重新test = re.search(r'(\b[A-Z]([a-z])*\b){2,}',line)打印(布尔(测试))
但是打印False
.如果我改为
test = re.search(r'(\b[A-Z]([a-z])*\b)',line)
我发现 print(test.group(1))
是 Cow
但 print(test.group(2))
是 w
,第一个匹配的最后一个字母(test.group
中没有其他元素).
对于查明此问题和/或如何更好地解决问题有任何建议吗?
由于内括号,匹配的最后一个字母在组中.放下那些,你会没事的.
>>>t = re.findall('([A-Z][a-z]+)', 行)>>>吨['牛','苹果','汪']>>>t = re.findall('([A-Z]([a-z])+)', 行)>>>吨[('Cow', 'w'), ('Apple', 'e'), ('Woof', 'f')]大写单词的数量当然是len(t)
.
I'm trying to learn how to use regular expressions but have a question. Let's say I have the string
line = 'Cow Apple think Woof`
I want to see if line
has at least two words that begin with capital letters (which, of course, it does). In Python, I tried to do the following
import re
test = re.search(r'(\b[A-Z]([a-z])*\b){2,}',line)
print(bool(test))
but that prints False
. If I instead do
test = re.search(r'(\b[A-Z]([a-z])*\b)',line)
I find that print(test.group(1))
is Cow
but print(test.group(2))
is w
, the last letter of the first match (there are no other elements in test.group
).
Any suggestions on pinpointing this issue and/or how to approach the problem better in general?
The last letter of the match is in group because of inner parentheses. Just drop those and you'll be fine.
>>> t = re.findall('([A-Z][a-z]+)', line)
>>> t
['Cow', 'Apple', 'Woof']
>>> t = re.findall('([A-Z]([a-z])+)', line)
>>> t
[('Cow', 'w'), ('Apple', 'e'), ('Woof', 'f')]
The count of capitalised words is, of course, len(t)
.
这篇关于正则表达式 - 在字符串中查找大写单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!