wood burning stoves 2.0*
The moose likes Java in General and the fly likes Removing Smileys from Chatlog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Removing Smileys from Chatlog" Watch "Removing Smileys from Chatlog" New topic
Author

Removing Smileys from Chatlog

Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
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

Joined: Jul 18, 2011
Posts: 287
what is the delimiter? And why not add smilies to a set?
Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
With the following code you don't have to use any list/set and so no traversing the collection
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7499
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
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

Joined: Mar 17, 2011
Posts: 7499
    
  18

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing Smileys from Chatlog
 
Similar Threads
calling a method in hibernate DAO through JMX export MBean
seam web remoting ArrayList problem
<tx:annotation-driven /> giving error
iterate problem in struts2
Inconsistent stack height 2 != 1