1 2 Previous Next 25 Replies Latest reply: Apr 28, 2009 8:07 AM by 796440 RSS

    how to remove all regex in a string?

    800332
      hi guys,

      for instance, if you have a string like below :

      String s = "abcd!@3456*&^%()-+";

      how can I remove all the regex and left and numeric and alphabet only?

      I don't really know how to use the java.util.regex !

      Thanks,
      Mark
      Sdalk
        • 1. Re: how to remove all regex in a string?
          800308
          So you remove everything that is (not-a-letter and not-a-digit) using an RE... which in practice boils down to: retain everything that (is-a-letter or is-a-digit).

          You need to put in an effort your self... Use the tutorials... and read the [API doco|http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html], which (in this case) is an excellent reference.
          • 2. Re: how to remove all regex in a string?
            800282
            kmthien wrote:
            hi guys,

            for instance, if you have a string like below :

            String s = "abcd!@3456*&^%()-+";

            how can I remove all the regex and left and numeric and alphabet only?
            I know what regex is, but what you you mean by "the regex"?
            What would be the output of the example String?
            • 3. Re: how to remove all regex in a string?
              800332
              ok the string will end up like :

              "abcd3456";

              after removing all the )(*&^%$#@!
              • 4. Re: how to remove all regex in a string?
                800308
                How are ya' Bart... Obviously this dude barely speaks English, so lets not expect a clear problem definition... Tain't gonna happen.
                • 5. Re: how to remove all regex in a string?
                  807588
                  Hello you can easily use this
                  import java.util.ArrayList;
                  
                  public class RemoveFromString {
                       static char AllowedChars[] = {'a','b','c','d','e','f','g','h','i','1','2','3'}; // here write all chars that are allowed
                       static ArrayList Allowed = new ArrayList();
                       public static void main(String args[]) {
                            addToList();
                            String s = "abcd!@3456*&^%()-+";
                            char ch[] = s.toCharArray();
                            String filtredString = "";
                            for(char cha : ch) {
                                 if(Allowed.contains(cha)) {
                                      filtredString += cha;
                                 }
                            }
                            System.out.println(filtredString);
                       }
                       public static void addToList() {
                            for(char ch : AllowedChars) {
                                 Allowed.add(ch);
                            }
                       }
                  }
                  • 6. Re: how to remove all regex in a string?
                    800308
                    Well I'll eat my shorts ;-)

                    "abcd!@3456*&^%()-+" ==> retain everything that (is-a-letter or is-a-digit) ==> "abcd3456"

                    This might help: http://www.regexbuddy.com/create.html
                    • 7. Re: how to remove all regex in a string?
                      800282
                      kmthien wrote:
                      ok the string will end up like :

                      "abcd3456";

                      after removing all the )(*&^%$#@!
                      Okay, those characters aren't all regex-related.
                      Anyway, you could use String's replaceAll(String, String) method. The first parameter should be a regex-String (the pattern) and the second parameter should be a replacement String. In your case the regex can be a "negated character class": that way you can say "replace all characters that are NOT a-z or 0-9".

                      Have a look at this regex tutorial about character classes:
                      [http://www.regular-expressions.info/charclass.html]

                      And please don't post back immediately asking for code, it'd be nice if you'd try it yourself first after reading the tutorial I posted.

                      Good luck.
                      • 8. Re: how to remove all regex in a string?
                        800282
                        Juraj wrote:
                        Hello you can easily use this
                        ...
                        I hope not!
                        It's not very well written code: and it makes an awful lot of unnecessary contains(...) calls.
                        • 9. Re: how to remove all regex in a string?
                          800308
                          Juraj,

                          No offence (I know you're trying to help) but dude, You evidently know diddly about programming... Do you know anything about gardening?

                          Cheers. Keith.
                          • 10. Re: how to remove all regex in a string?
                            800282
                            corlettk wrote:
                            How are ya' Bart...
                            I'm good, and you?
                            Not sure if you have a watch set on the thread, but I replied to your knapsack solution from yesterday (or was it the day before yesterday?).
                            • 11. Re: how to remove all regex in a string?
                              807588
                              Im not sure what you dont like on that code. At least it works right. It is the simpliest way how to filter Strings and i wrote it like that because you can choose what chars you want or no, so you can use it not only for alphabet and numeric filtring
                              corlettk wrote:
                              Juraj,

                              No offence (I know you're trying to help) but dude, You evidently know diddly about programming... Do you know anything about gardening?

                              Cheers. Keith.
                              • 12. Re: how to remove all regex in a string?
                                800282
                                Juraj wrote:
                                Im not sure what you dont like on that code.
                                Here are a couple of things I don't like about it:
                                - you didn't follow normal [Code Conventions|http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html] making your code hard to read;
                                - it's overly complicated for such a simple task. By complicated I mean it is not clear what that code is supposed to do when reading through it;
                                - you stuffed the majority of that code in your main method instead of creating a separate method;
                                - your algorithm loops through the AllowedChars array too much;
                                - you're concatenating Strings instead of using some sort of buffer.
                                At least it works right. It is the simpliest way how to filter Strings
                                No, replaceAll(...) is far, far simpler.
                                • 13. Re: how to remove all regex in a string?
                                  807588
                                  Hmm it was only example how you can filter it

                                  If you say that "replaceAll(...) is far, far simpler " then please can you write the code how it will look like?
                                  prometheuzz wrote:
                                  Juraj wrote:
                                  Im not sure what you dont like on that code.
                                  Here are a couple of things I don't like about it:
                                  - you didn't follow normal [Code Conventions|http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html] making your code hard to read;
                                  - it's overly complicated for such a simple task. By complicated I mean it is not clear what that code is supposed to do when reading through it;
                                  - you stuffed the majority of that code in your main method instead of creating a separate method;
                                  - your algorithm loops through the AllowedChars array too much;
                                  - you're concatenating Strings instead of using some sort of buffer.
                                  At least it works right. It is the simpliest way how to filter Strings
                                  No, replaceAll(...) is far, far simpler.
                                  • 14. Re: how to remove all regex in a string?
                                    800282
                                    Juraj wrote:
                                    Hmm it was only example how you can filter it
                                    Yes, I know. And IMO, it was a bad example.
                                    Note that I'm not criticising you, but your code (there's a big difference!). By posting code on a public forum, you must be prepared to get comments about it. If the critique about it is unfounded, feel free to say so, otherwise just take it and learn from it.
                                    If you say that "replaceAll(...) is far, far simpler " then please can you write the code how it will look like?
                                    I will wait for the OP to give it a try first. I don't want to spoil his/her learning experience!
                                    ; )
                                    1 2 Previous Next