I am trying to split strings into lists of "tags" in python. The splitting should handle strings such as "HappyBirthday" and remove most punctuation but preserve hyphens, and apostrophes. My starting point is:
tags = re.findall("([A-Z]{2,}(?=[A-Z]|$)|[A-Z][a-z]*)|\w+-\w+|[\w']+"
I would want to turn this sample data:
Jeff's dog is un-American SomeTimes! BUT NOTAlways
Into:
['Jeff's', 'dog', 'is', 'un-American', 'Some', 'Times', 'BUT', 'NOT', 'Always']
P.S. I am sorry my description isn't very good. I am not sure how to explain it, and have been mostly unsuccessful with google. I hope the example illustrates it properly.
Edit: i think i needed to be more precise, so also,
I suggest the following:
re.findall("[A-Z]{2,}(?![a-z])|[A-Z][a-z]+(?=[A-Z])|[\'\w\-]+",s)
This yields for your example:
["Jeff's", 'dog', 'is', 'un-American', 'Some', 'Times', 'BUT', 'NOT', 'Always']
Explanation: The RegExp is made up of 3 alternatives:
[A-Z]{2,}(?![a-z])
matches words with all letters capital[A-Z][a-z]+(?=[A-Z])
matches words with a first capital letter. The lookahead (?=[A-Z])
stops the match before the next capital letter[\'\w\-]+
matches all the rest, i.e. words which may contain '
and -
.