• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing Smileys from Chatlog

 
Ankit Chandrawat
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There is a requirement wherein I need to remove smileys from the chat Transcript that are in the form of a String.

The smileys are as following that I have gathered in a List object.



I am getting a PatternSyntaxException, and that is due to the fact that the metacharacters <([{\^-=$!|]})?*+.> needs to be treated with a \\ . What is the best possible way to check for each and every smiley and then replacing it.

Also a chatRow is our regular java String.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the delimiter? And why not add smilies to a set?
 
Ankit Chandrawat
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya I am open to assign smileys to set, but there is no delimiter as such. That is why I am using contains. At most we can consider space as the delimiter.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Chandrawat wrote:

There's an easy way to escape all regular expression meta characters:
However, that's not necessary here. Since Java 5.0, String has an extra replace method that replaces one CharSequence with another. Since String implements CharSequence you can use this:
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the following code you don't have to use any list/set and so no traversing the collection
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:With the following code you don't have to use any list/set and so no traversing the collection...

Ooof. I'm not sure I like that. Seems awfully arcane.

Why not just use a HashMap<String, Pattern>? viz, from OP's own code:of course you might be able to make it a little more efficient by using a LinkedHashMap and adding smileys in a sequence that avoids match collisions, but I leave that up to OP .

Winston
 
Ankit Chandrawat
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to all of you for the great replies to my query. Now I am confused what solution should I go for as I like each one of them. Thanks again !!!
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Chandrawat wrote:Now I am confused what solution should I go for as I like each one of them. ...

I can't possibly comment, being a biased contributor; but there is one thing that may be worth mentioning:
A naive replaceAll(<smiley pattern>, "") may end up leaving your chat lines with superfluous spaces.
Hopefully this isn't an issue (and it's easily remedied), but you should be aware of it.

Unfortunately, the Pattern in my solution can't check for it, since all it knows about is the smiley itself; not where it's placed.
Harsha's solution could be made context-sensitive; but I suspect at the expense of a longer and more brittle expression.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic