ruby - How do I match a UTF-8 encoded hashtag with embedded punctuation characters? -
i want extract #hashtags string, have special characters such #1+1.
currently i'm using:
@hashtags ||= string.scan(/#\w+/) but doesn't work special characters. also, want utf-8 compatible.
how do this?
edit:
if last character special character should removed, such #hashtag, #hashtag. #hashtag! #hashtag? etc...
also, hash sign @ beginning should removed.
this should work:
@hashtags = str.scan(/#([[:graph:]]*[[:alnum:]])/).flatten or if don't want hashtag start special character:
@hashtags = str.scan(/#((?:[[:alnum:]][[:graph:]]*)?[[:alnum:]])/).flatten
Comments
Post a Comment