Skip to content

Instantly share code, notes, and snippets.

@JKirchartz
Last active December 17, 2015 04:28
Show Gist options
  • Save JKirchartz/5550316 to your computer and use it in GitHub Desktop.
Save JKirchartz/5550316 to your computer and use it in GitHub Desktop.
Transform words in a phrase to hashtags using a whitelist, without changing the upper/lower case characters in the words in the phrase (i.e. if your phrase has "Beer" but your whitelist has "beer", you'll see "#Beer")
import re
whitelist = ['quick','lazy','boring']
tweet = "The quick brown fox jumped over the lazy dog. The Quick Brown Fox Jumped Over The Boring Dog"
for item in whitelist:
tweet = re.compile('('+item+')',re.I).sub(r'#\1', tweet, 1)
# remove the `1` if you want it to match all instances
print tweet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment