Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Capital words between words – Regex

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]*

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading