Home > other >  Python Regex: Find all caps with no following lowercase
Python Regex: Find all caps with no following lowercase

Time:08-26

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'

CodePudding user response:

Try (regex101):

import re

test1 = "ThisIsATestTHISISATestTHISISATEST"

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

Prints:

['THISISA', 'THISISATEST']
  • Related