0

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.

Community
  • 1
  • 1
verby
  • 1

2 Answers2

0

You can use regular expressions to do this:

import re
words = re.findall('[A-Z][a-z]*', 'TheWords')
Daniel Egeberg
  • 8,359
  • 31
  • 44
  • This regexp won't work for non-ASCII letters. – Jacek Konieczny Jun 25 '10 at 07:55
  • @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 Jun 25 '10 at 08:09
0

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
  • 34,669
  • 9
  • 84
  • 115