Split string on non consecutive capital letters

Viewed 476

I'm trying to split a string by capital letter BUT i don't want to split two consecutive capital letters.

So for now I'm doing this:

my_string == "TTestStringAA"
re.findall('[a-zA-Z][^A-Z]*', my_string)
>>> ['T', 'Test', 'String', 'A', 'A']

But the output that I'm looking for is:

>>> ['TTest', 'String', 'AA']

There is a clean and simple solution to this problem?

Thx!

3 Answers

I believe [A-Z]+[a-z]* meets your requirements:

>>> re.findall(r'[A-Z]+[a-z]*', my_string)
['TTest', 'String', 'AA']

The following regex will return the correct result.

[a-z]*[A-Z]+[a-z]*|[a-z]+$

Test Cases:

tests = ['a', 'A', 'aa', 'Aa' 'AaAaAAAaAa', 'aTTestStringAA']
regex = re.compile(r'[a-z]*[A-Z]+[a-z]*|[a-z]+$')
for test in tests:
    print('{} => {}'.format(test, re.findall(regex, test)))

Use re.split with

(?<=[a-z])(?=[A-Z])

See proof.

Explanation

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead

Python code:

import re
pattern = r"(?<=[a-z])(?=[A-Z])"
test = "TTestStringAA"
print(re.split(pattern, test))

Results:

['TTest', 'String', 'AA']
Related