![](/img/trans.png)
[英]How can I use Regex to abbreviate words that all start with a capital letter
[英]how can i find the words in a string that start with capital letter?
如何找到以大寫字母開頭的字符串中的單詞?
示例輸入:
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."
預期 output:
Persian League Iran Persian League
假設您也可以接受The
和This
:
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)
給
['The', 'Persian', 'League', 'Iran', 'The', 'Persian', 'League', 'This']
如果您需要忽略The
和This
:
matches = re.findall("(?!The|This)([A-Z].+?)\W", input_string)
給
['Persian', 'League', 'Iran', 'Persian', 'League']
沒有正則表達式:
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:
['The', 'Persian', 'League', 'Iran.', 'The', 'Persian', 'League']
如果你想跳過The
詞(或任何其他詞)試試這個:
print(" ".join(w.replace(".", "") for w in txt.split() if w[0].isupper() and w not in ["The", "This"]))
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() ])
嘗試這個:
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))
結果:
['The', 'Persian', 'League', 'Iran', 'The', 'Persian', 'League']
另一種解決方法是使用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']
在我的示例中,我使用了str.isupper()和str.split() ,它們都是 Python 標准庫中的內置方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.