Thursday, 15 March 2012

regex - How To Replace Hashtags with HTML Anchor in Python/Django? -



regex - How To Replace Hashtags with HTML Anchor in Python/Django? -

message = "@me nice. @you: allow me #help you. #anyone> #python, #all."

message.split() gives "

['@me', 'this', 'is', 'nice.', '@you:', 'let', 'me', '#help', 'you.', '#anyone>', '#python,', '#all.']

but want

['@me', 'this', 'is', 'nice.', '@you', 'let', 'me', '#help', 'you.', '#anyone', '#python', '#all']. without :, ., > or other symbol. want words alone.

startswith('#') should homecoming

['#help', '#anyone', '#python', #all] , hashtag_links return

["<a href='hashtags\help'>#help</a>", "<a href='hashtags\anyone'>#anyone</a>", ...]

what want able replace hashtags in message equivalent in hashtag_links can clickable when rendered in html.

[\s.:>,]

you can split this.use re.split.remove blank groups.

see demo.

http://regex101.com/r/su3fa2/11

python regex django

No comments:

Post a Comment