I am trying to find capital words occurring between words using Regex. I want to ignore capital words after.?! and starting of paragraph.
Currently using the code below to find capital letters
[A-Z][^\s]*
Example
A sentence containing Capital letters. How to Extract only capital letters?
The Regex should find only Capital and Extract ignoring How and A
>Solution :
One possible solution, find all patterns, but use capturing group only on pattern you want to match (regex101):
import re
pat = re.compile(r"^\s*[A-Z]+|[.?!]\s*[A-Z]+|([A-Z]\S*)")
text = "A sentence containing Capital letters. How to Extract only capital letters?"
for m in pat.findall(text):
if m:
print(m)
Prints:
Capital
Extract