5 Replies Latest reply: Oct 30, 2007 6:13 AM by 796220 RSS

    Filter Strings using regular expressions.

    796220
      Requirements.
      1.I have a table with different names.
      2.I input a word(string) through a text box.
      3.I filter table using the input string through text box using the code
      ((DefaultRowSorter)table_customer.getRowSorter()).setRowFilter(RowFilter.regexFilter(regex, indices));
      4.regex is obtained as follows.
      String regex = "";
      String text = txtFilterText.getText();


      regex = "^(?i)"+text+".*"; //for starts with filter
      regex = ".*" + text + ".*";//for contains filter
      regex = "^(?i)[^"+text+".*]";//for doesnt start with filter
      regex =".*(?i)"+text+"$";//for end with filter


      I need help for doesnt contain and doesnt end with filters.Plz help me out..
      Anees
        • 1. Re: Filter Strings using regular expressions.
          807603
          Check the string using method "find" of a Matcher object where the Regex Pattern should be in a capturing group. This way you will get the start/end position of pattern occurence.
          • 2. Re: Filter Strings using regular expressions.
            796220
            I dont have a string in hand..I am just embedding the word entered through text box along with pattern, to regex, which is a string..

            ((DefaultRowSorter)table_customer.getRowSorter()).setRowFilter(RowFilter.regexFilter(regex, indices)); ------does the filtering for me as per the given pattern.
            it works for
            regex = "^(?i)"text".*"; //for starts with filter
            regex = "." + text + ".";//for contains filter
            regex = "(?i)["text".*]";//for doesnt start with filter
            regex =".*(?i)"text"$";//for end with filter
            I need to get doesnt contain and doesnt end with..
            Plz anyone help with code plz...
            • 3. Re: Filter Strings using regular expressions.
              807603
              Whenever you include source code in a post, you have to enclose it in CODE tags or it gets completely mangled by the forum software.
              regex = "(?i)^\\Q" + text "\\E.*$";     // starts with
              regex = "(?i)^.*\\Q" + text + "\\E$";   // ends with
              regex = "(?i)^.*\\Q" + text + "\\E.*$"; // contains
              
              regex = "(?i)^(?!\\Q" + text "\\E).*$";       // doesn't start with
              regex = "(?i)^.*(?<!\\Q" + text + "\\E)$";    // doesn't end with
              regex = "(?i)^(?:(?!\\Q" + text + "\\E).)*$"; // doesn't contain
              • 4. Re: Filter Strings using regular expressions.
                796220
                Whole hearted thanks..this is more than a help..n these were beyond my wildest imaginations..
                i needed one more..equals filter...
                once again..thx
                Anees Ahamed
                • 5. Re: Filter Strings using regular expressions.
                  796220
                  OK..equals filter was simple n I got the same..
                  Thx