Mr_Hmp
Mr_Hmp

Reputation: 2535

Detect which emoticon is present in given string

I am making a java application and I require to remove the emoticon present with the 'keyword' or 'emotion' which that emoticon is representing....

For example

I am feeling very happy :)

should be changed to

I am feeling very happy smile.

How can I do it, I tried using regex but I am not able to design generalised regex pattern for this.

Upvotes: 0

Views: 769

Answers (3)

Marko Topolnik
Marko Topolnik

Reputation: 200206

The use case you have is optimally solved by using a trie data structure to hold all your emoticons. At each step through the string you make one step down the trie (if there's a match for the char). This would allow you to make just one pass through the string to find all occurences of emoticons.

Upvotes: 3

Claude
Claude

Reputation: 71

A generalized regex won't really help unless you're swapping all emoticons with a single phrase. What you need is a dictionary of the form ":)"=>"smile",":("=>"frown". Once that's complete, you can use String.replace(String, String) and not even worry about a regex.

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39395

what about using Hashmap to indexing the emoticons? then instead of regex, just perform a search replace!

Upvotes: 0

Related Questions