Python正则表达式,如何搜索以大写开头的单词? [英] Python regular expressions, how to search for a word starting with uppercase?
本文介绍了Python正则表达式,如何搜索以大写开头的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 Python 比较陌生,刚开始学习正则表达式.我似乎无法弄清楚如何找到以大写字母开头的单词.例如:
text = '>:{abcd|}+)_(#)_@_Mitch_(@<$)_)*zersx!)Pamela@(_+)('m = re.match(r'(\w+) (\w+)', 文本)
我希望它只返回 ('Mitch', 'Pamela')
.
- 如何使用
re.match()
执行此操作? - 是否可以使用
re.split()
?
解决方案
您可以简单地使用 re.findall
仅包含字母模式(因为 \w
组也将匹配 _
字符).
I'm relatively new to Python, and I just started learning regular expressions. I can't seem to figure how to find a word that starts with an uppercase. For example:
text = '>:{abcd|}+)_(#)_@_Mitch_(@<$)_)*zersx!)Pamela@(_+)('
m = re.match(r'(\w+) (\w+)', text)
I would like it to just return ('Mitch', 'Pamela')
.
- How do I do this using
re.match()
? - Is it possible to do with
re.split()
?
解决方案
You can simply use re.findall
with just the letter pattern (as the \w
group will also match the _
character).
>>> re.findall('[A-Z][A-Za-z]*', text)
['Mitch', 'Pamela']
这篇关于Python正则表达式,如何搜索以大写开头的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文