Python Regex: Find all caps with no following lowercase

Question:

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'

Asked By: John Stud

||

Answers:

Try (regex101):

import re

test1 = "ThisIsATestTHISISATestTHISISATEST"

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

Prints:

['THISISA', 'THISISATEST']
Answered By: Andrej Kesely
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.