简体   繁体   English

仅提取首字母大写的整个单词

[英]Extract Only Whole Word That Has The First Letter Capitalized

I have a text file need to be analyzed here, what I am interested is only the whole word with the first letter capitalized,我这里有一个文本文件需要分析,我感兴趣的只是第一个字母大写的整个单词,

For example: test string: Everyday HOLDS the poSSibility Of A Miracle例如:测试字符串: Everyday HOLDS the poSSibility Of A Miracle

I want to capture: Everyday Of A Miracle我想捕捉: Everyday Of A Miracle

I am currently trying to build my regular expression in Python, strangely, my regex only can capture the first whole word that is captalized.我目前正在尝试在 Python 中构建我的正则表达式,奇怪的是,我的正则表达式只能捕获第一个大写的整个单词。

Test String: Everyday HOLDS the poSSibility Of A Miracle测试字符串: Everyday HOLDS the poSSibility Of A Miracle

My regex: ^([AZ])?([az])+我的正则表达式: ^([AZ])?([az])+

Capture: Everyday捕获: Everyday

What am I missing here ?我在这里错过了什么?

Instead of anchoring the regex at the beginning of the string, utilize boundary checking:不是将正则表达式锚定在字符串的开头,而是利用边界检查:

import re
s = 'Everyday HOLDS the poSSibility Of A Miracle'
new_s = ' '.join(re.findall(r'\b[A-Z][a-z]+|\b[A-Z]\b', s))

Output:输出:

'Everyday Of A Miracle'

Without regex (only if words are delimited by whitespaces):没有正则表达式(仅当单词由空格分隔时):

>>> s='Everyday HOLDS the poSSibility Of A Miracle'
>>> [x for x in s.split() if x.title()==x]
['Everyday', 'Of', 'A', 'Miracle']

Note that you can also use re.split to split on any non-letter characters.请注意,您还可以使用 re.split 拆分任何非字母字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首字母分隔并大写 - First Letter separated and capitalized Python 新手:如何保持每个单词的首字母大写? - New to Python: How to keep the first letter of each word capitalized? 提取除第一个单词以外的大写单词的句子 - Extract sentences with capitalized words other than first word 字符串变量只接受字符串的第一个字母,而不是整个单词 - string variable only ever taking the first letter of the string instead of the whole word 使用正则表达式提取第一个字母单词 - Using regex to extract first one letter word 使用SQLAlchemy保存到mysql后,每个单词的首字母大写 - First letter of every word gets capitalized after saving to mysql using SQLAlchemy 如果单元格有 2 个单词,则只提取第一个单词,如果单元格有 3 个单词,则提取第一个单词 - PANDAS/REGEX - If cell has 2 words, extract only 1st word and if cell has 3 words, extract 2 first words - PANDAS/REGEX 使“终止”的每个版本都相同-大写=小写=大写的第一个字母=大写的任何字母 - Make every version of “cease” the same - uppercase = lowercase = capitalized first letter = any letter capitalized 提取“全名”字段中最后一个单词的第一个字母。 - Python - Extract first letter of the last word in "full name" field. - Python Python - 需要从单词字符串中提取第一个和最后一个字母 - Python - Need to extract the first and last letter from a word string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM