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

Python Regex: Find all caps with no following lowercase

How can I retain all capital characters, given that subsequent characters are not lower case?

Consider this example:

import re
test1 = 'ThisIsATestTHISISATestTHISISATEST'

re.findall(r'[A-Z]{2}[^a-z]+', test1)
# ['THISISAT', 'THISISATEST']

Expectation:
This: 'THISISAT', should read: 'THISISA'

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

>Solution :

Try (regex101):

import re

test1 = "ThisIsATestTHISISATestTHISISATEST"

print(re.findall(r"[A-Z]{2}[A-Z]*(?![a-z])", test1))

Prints:

['THISISA', 'THISISATEST']
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