tags:

views:

58

answers:

2

Possible Duplicate:
Python: Split a string at uppercase letters

I'm trying to figure out how to change TwoWords into Two Words and I can't think of a way to do it. I need to split based on where it's capitalized, that will always be a new word. Does anyone have any suggestions?

In python.

A: 

You can use regular expressions to do this:

import re
words = re.findall('[A-Z][a-z]*', 'TheWords')
Daniel Egeberg
This regexp won't work for non-ASCII letters.
Jacek Konieczny
@Jacek Konieczny - Since Python doesn't let you match based on unicode character properties, you either have to limit yourself to ASCII, or manually include characters and character ranges from languages you want to support. See http://stackoverflow.com/questions/1832893/python-regex-matching-unicode-properties
gnud
A: 

You can use regular expressions:

import re
re.findall("[A-Z][a-z]*","TwoWordsAATest")

re.findall("[A-Z][^A-Z]*","TwoWordsAATest")

http://docs.python.org/library/re.html

phimuemue