BlackBeltPanda

RegexEmoticon

May 15th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. Example emoticon system that replaces ascii emoticons with unicode emoticons:
  2.  
  3. #Replaces (.V.) with (\/) (°,,,°) (\/)
  4. match [^\b\B]\(\.V\.\)[\b\B$]
  5. then replace (\/) (°,,,°) (\/)
  6.  
  7. #Replaces #-o with (ʘ言ʘ╬)
  8. match [^\b\B]\#\-o[\b\B$]
  9. then replace (ʘ言ʘ╬)
  10.  
  11. #Replaces B-) or B) with ༼⌐■ل͟■༽
  12. match [^\b\B]B(?:\-)?\)[\b\B$]
  13. then replace ༼⌐■ل͟■༽
  14.  
  15. #Replaces :disappoint: with ಠ_ಠ
  16. match [^\b\B]\:disappoint\:[\b\B$]
  17. then replace ಠ_ಠ
  18.  
  19. Explanation of regex:
  20. "[^\b\B]" matches "^", which is the start of a string or line, "\b", which is a word boundary, or "\B", which is a non-word boundary.
  21. "[\b\B$]" matches "\b", which is a word boundary, "\B", which is a non-word boundary, or "$", which is the end of a string or line.
  22. The above two pieces of regex ensure that we don't accidentally match characters that are part of a word.
  23. We use "\" a lot to escape characters like "." so it doesn't think we're trying to use them as regex. By using "\.", we can search for ".".
  24. With the sunglasses emoticon, we used "(?:\-)?". This is a passive group that matches the "-" character. The "?" at the end means it will match 0 or 1 of that character, making it optional so we can match both "B-)" and "B)".
Advertisement
Add Comment
Please, Sign In to add comment