Fox
Fox

Reputation: 9444

Retain emoticons, Java Regex

I have a program which filters the string and retains the english characters and the emoticons. I am trying to get a regular expression which keeps the emoticons like :) , :D , :( etc but takes out single ':' or '(' or ')' ... Basically I want ':' and ')' together else I need to filter them....In my program I am able to keep the emoticons but I am also getting : and ) along with it....Can you please help me out?

String pattern = "[^\\w^\\s^(:))]";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
text = m.replaceAll("");

Thanks for your help.

Upvotes: 0

Views: 626

Answers (1)

Adam Zalcman
Adam Zalcman

Reputation: 27233

You're trying to use grouping parenthesis inside square brackets. This doesn't work since inside square brackets parenthesis lose their special meaning.

Square brackets define a character class which is a single atom, not a sequence of atoms. Instead, you should simply use a two-branch alternative: one for : and one for a parenthesis, D etc and use look-ahead and look-behind in each branch.

You can find more info about regular expression syntax here.

Also, you may give some consideration to more complex emoticons like :-).

Upvotes: 1

Related Questions