简体   繁体   English

如何找到以大写字母开头的字符串中的单词?

[英]how can i find the words in a string that start with capital letter?

How can I find the words in a string that start with a capital letter?如何找到以大写字母开头的字符串中的单词?

Example input:示例输入:

input_str = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship. This video was captured by one of our heroes who wishes peace."

Expected output:预期 output:

Persian League Iran Persian League

Assuming you can accept The and This as well:假设您也可以接受TheThis

import re
input_string = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship. This video was captured by one of our heroes who wishes peace."
matches = re.findall("([A-Z].+?)\W", input_string)

gives

['The', 'Persian', 'League', 'Iran', 'The', 'Persian', 'League', 'This']

If you need to ignore The and This :如果您需要忽略TheThis

matches = re.findall("(?!The|This)([A-Z].+?)\W", input_string)

gives

['Persian', 'League', 'Iran', 'Persian', 'League']

Without regex:没有正则表达式:

txt = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship."

print([w for w in txt.split() if w.istitle()])

Output: Output:

['The', 'Persian', 'League', 'Iran.', 'The', 'Persian', 'League']

If you want to skip the The word (or any other word for that matter) try this:如果你想跳过The词(或任何其他词)试试这个:

print(" ".join(w.replace(".", "") for w in txt.split() if w[0].isupper() and w not in ["The", "This"]))

Output: Output:

Persian League Iran Persian League
s = """
The Persian League is the largest sport event dedicated to the deprived areas 
of Iran. The Persian League promotes peace and friendship. This video was 
captured by one of our heroes who wishes peace.
"""
print( [ x for x in s.split() if x[0].isupper() ])

Try this:尝试这个:

import re
inputString = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship."
splitted = re.split(' |\.', inputString)
result = filter(lambda x: len(x) > 0 and x[0].isupper(), splitted)
print(list(result))

Result:结果:

['The', 'Persian', 'League', 'Iran', 'The', 'Persian', 'League']

Another way to solve is using for to read data and put the words with capital letters in a list.另一种解决方法是使用for读取数据并将带有大写字母的单词放入列表中。

phrase = 'The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship. This video was captured by one of our heroes who wishes peace.'

wordsplit = phrase.split(' ')
capitalLettersWords = []
for word in wordsplit:
    if word[0].isupper():
        capitalLettersWords.append(word)

print(capitalLettersWords)
#['The', 'Persian', 'League', 'Iran.', 'The', 'Persian', 'League', 'This']

In my example I used the str.isupper() and str.split() , both built-in methods from Python standard lib.在我的示例中,我使用了str.isupper()str.split() ,它们都是 Python 标准库中的内置方法。

暂无
暂无

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

相关问题 如何使用正则表达式缩写所有以大写字母开头的单词 - How can I use Regex to abbreviate words that all start with a capital letter 用于检测以字符串中的大写字母开头的所有单词的代码 - Code to detect all words that start with a capital letter in a string 如何查找单词 - 第一个字母大写,其他字母小写 - How to find a words - First letter will be capital & other will be lower 如何使用正则表达式从 NLTK 语料库中查找大写字母单词? - How to find a capital letter words from an NLTK corpus using regex? 如何在字符串中搜索大写字母并返回带和不带大写字母的单词列表 - how to search for a capital letter within a string and return the list of words with and without capital letters 查找以大写字母作为起始字母但前面没有空格的单词 - find words with capital letter as starting letter but not preceded by space 使用正则表达式查找不是在句子开头的大写字母 - Find words with capital letters not at start of a sentence with regex 如何在包含至少一个下划线和大写字母的字符串中查找单词 - How to find words in a string containing at least one underscore and capital letters 如何识别 Python 中是否至少有一个大写字母? - How can I identify if there is, at least, one capital letter in Python? 在2个大写字母(regex)之前找到以大写字母开头的n个单词 - Find n words starting with capital letter before 2 words of capital letters (regex)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM