11 Replies Latest reply: Jan 14, 2009 6:26 AM by JoachimSauer RSS

    Removing square brackets from a String

    807589
      Hi everyone, i am using this code to clean from stirng unwanted characters.

      String str;
      // using regular expression to eliminate disallowed characters such as (!,",;... Global.disAllowedCharacter)
      // from incoming string
      str = text.replaceAll("[*\"'^’+%&/()“=?!_#½{}\\|-~`;,´.:<>\\n\\r ]+", Global.replacer);
      But I cannot remove [] characters from a string.

      How can i remove "[","]" square brackets and "$" characters from string?
      Thanks for responses.
        • 1. Re: Removing square brackets from a String
          807589
          Quote it?
          import java.util.regex.Pattern;
          
          public class Example {
              public static void main(String[] args) {
                  String illegals = "[]$^*\"'^’+%&/()“=?!_#½{}\\|-~`;,´.:<>\n\r";
                  String pattern = "[" + Pattern.quote(illegals) + "]";
          
                  String text = "This [is] a $small example";
                  String result = text.replaceAll(pattern, "");
                  System.out.format("%s->%s%n", text, result);
              }
          }{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
          • 2. Re: Removing square brackets from a String
            807589
            Hi Futi,
            I'm pretty new to the forum but could you explain in a little more detail the issue you are having. I think I know what it is but can you clarify please, I don't want to look stupid on my first post! Thanks in advance, Mark.

            [Business networking|http://www.ukifp.org/about-mark-harrison]
            • 3. Re: Removing square brackets from a String
              807589
              Then again, it looks like you're trying to remove all "non-word" characters:
              public class Example {
                  public static void main(String[] args) {
                      String text = "This [is] a $small e-x-ample";
                      String result = text.replaceAll("[\\W]", "");
                      System.out.format("%s->%s%n", text, result);
                  }
              }
              I suggest you go through the documentation of Pattern to review regex: [http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html]
              • 4. Re: Removing square brackets from a String
                807589
                You can escape square brackets too!
                String data = "hello [world]";
                System.out.print(data.replaceAll("[\\]\\[]", ""));
                • 5. Re: Removing square brackets from a String
                  807589
                  \W won't work for two reasons: it won't match the underscore because that's considered a word character, and it won't match the curly quotes because they're not ASCII characters. I would suggest this:
                  String result = text.replaceAll("\\P{Alnum}+", "");
                  The differences among the pre-defined classes are subtle, but important:
                  "\\w"        // word character: [A-Za-z0-9_]
                  "\\W"        // any ASCII character that isn't a word character
                  "[^\\w]"     // any Unicode character that isn't a (ASCII) word character
                  "\\p{Alnum}" // ASCII letter or digit: [A-Za-z0-9]
                  "\\P{Alnum}" // any Unicode character except those matched by \p{Alnum}
                  • 6. Re: Removing square brackets from a String
                    DarrylBurke
                    markyh2904

                    This is a discussion forum, not somewhere to post an advertisement for your Business Networking.

                    I'm blocking your post, and shall block your user account if you continue to post links.

                    db
                    • 7. Re: Removing square brackets from a String
                      807589
                      who did try to post an advertisement link? Anyway, i am thanking all of responses owners.
                      • 8. Re: Removing square brackets from a String
                        800282
                        fuatsungur wrote:
                        who did try to post an advertisement link? ...
                        Like Darryl said: markyh2904 (reply #2).
                        • 9. Re: Removing square brackets from a String
                          807589
                          i am developing a Turkish text cleaner and "\\P" "\\W" all of them ignore the Turkish character like "Ü,&#304;,&#286;,&#350;,ü,&#305;,&#351;,ç,ö,Ö,Ç,&#287;". how can i prevent it?
                          • 10. Re: Removing square brackets from a String
                            799716
                            Something like this?
                            str.replaceAll("[^\\w"+(char)192+"-"+(char)255+"]", ""));
                            • 11. Re: Removing square brackets from a String
                              JoachimSauer
                              fuatsungur wrote:
                              i am developing a Turkish text cleaner and "\\P" "\\W" all of them ignore the Turkish character like "Ü,&#304;,&#286;,&#350;,ü,&#305;,&#351;,ç,ö,Ö,Ç,&#287;". how can i prevent it?
                              The universal solution is something like this:
                              str = str.replaceAll("[^\\p{L}\\p{Z}]","")
                              This removes anything that's not a Letter or a separator.