Home > Mobile >  Catch some words only
Catch some words only

Time:10-17

I have a paragraph (multiple sentences) and I want to catch all of the capitalised words in each sentence, except for the first word, even tough it is capitalised.

str = "He is a Good boy. When he sings Bob wants to listen. Bravo!"

Expected output:

>>> Good, Bob

CodePudding user response:

import re

string = "He is a Good boy. When he sings Bob wants to listen. Bravo!"

print([i.strip() for i in re.findall(r'\b [A-Z][a-zA-Z]*\b',string)])

#output: ['Good', 'Bob']

strip() is for getting rid of white space
\b [A-Z]' - this part takes all not first capital letters in a word that not first in a sentence
[a-zA-Z]*\b' - this part takes any letters after that firs capital letter

  • Related