Split string based on regex

user179169 picture user179169 · Nov 3, 2012 · Viewed 174.8k times · Source

What is the best way to split a string like "HELLO there HOW are YOU" by upper case words (in Python)?

So I'd end up with an array like such: results = ['HELLO there', 'HOW are', 'YOU']


EDIT:

I have tried:

p = re.compile("\b[A-Z]{2,}\b")
print p.split(page_text)

It doesn't seem to work, though.

Answer

Ωmega picture Ωmega · Nov 3, 2012

I suggest

l = re.compile("(?<!^)\s+(?=[A-Z])(?!.\s)").split(s)

Check this demo.