Python split string based on regex


Question

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.

1
88
5/24/2018 3:20:15 PM

Accepted Answer

I suggest

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

Check this demo.

103
11/3/2012 2:43:18 PM

You could use a lookahead:

re.split(r'[ ](?=[A-Z]+\b)', input)

This will split at every space that is followed by a string of upper-case letters which end in a word-boundary.

Note that the square brackets are only for readability and could as well be omitted.

If it is enough that the first letter of a word is upper case (so if you would want to split in front of Hello as well) it gets even easier:

re.split(r'[ ](?=[A-Z])', input)

Now this splits at every space followed by any upper-case letter.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon