10 Replies Latest reply: Nov 19, 2012 5:40 AM by AjayL RSS

    Using Regex Query to replace spaces.

    AjayL
      Hello

      I am new to Regex and amd trying to replace the space characters before and after the string without any trailing and leading spaces:

      For example :
      String word = "   Java  Regex      "; 
      
      Output Required: Java Regex
      I am writing the following code but it is not removing the spaces :
      public String test(){
      
           String regex = "(^)(\\s+) $";
           Pattern spacePattern = Pattern.compile(regex);
           Matcher spaceMatcher = spacePattern.matcher(criteria);
      
           String resultCriteria = "";
      
           if (spaceMatcher.find()) {
                resultCriteria = spaceMatcher.replaceAll(" ");
           }
      
           return resultCriteria.toLowerCase();
      }
      
      
      Assert.assertEquals("java Regex", searchNormalizer.normalize("   JaVa        REGex   "));   // Fail
      Ouput :  (space)JaVa        REGex(space)
      Can someone please advise the right way?

      Thanks
        • 1. Re: Using Regex Query to replace spaces.
          sabre150
          1) The regex should just be
          "\\s+"
          .
          2) There is no need to use find() since that is done behind the scenes in the Matcher.replaceAll().
          3) This
          resultCriteria = spaceMatcher.replaceAll(" ");
          says "replace all the spaces in a string that is just a space"! I would expect the argument to be the string referenced by 'word'!

          The whole can all be done in just one line using String.replaceAll() or in no more than 3 lines if one explicitly defines the Pattern and Matcher.
          • 2. Re: Using Regex Query to replace spaces.
            AjayL
            Hi Sabre

            I tried as per your suggestion :
            public String test(String criteria) {
            
               String regex = "\\s+";
                 
               Pattern spacePattern = Pattern.compile(regex);
               Matcher spaceMatcher = spacePattern.matcher(criteria);
            
               String resultCriteria = spaceMatcher.replaceAll(" ");
            
               return resultCriteria.toLowerCase();
            but there is still a space at the beginning and end of the criteria.
            Assert.assertEquals("java Regex", searchNormalizer.normalize("   JaVa        REGex   "));
            
            Output : <space>java regex <space>
            • 3. Re: Using Regex Query to replace spaces.
              sabre150
              Because you tell it to replace multiple space by one space and there are multiple spaces at the front and back of the original.
              • 4. Re: Using Regex Query to replace spaces.
                TPD-Opitz
                AjayL wrote:
                but there is still a space at the beginning and end of the criteria.>
                Assert.assertEquals("java Regex", searchNormalizer.normalize("   JaVa        REGex   "));
                Output : <space>java regex <space>
                To solve that look ahead/behind could be used:
                http://www.regular-expressions.info/lookaround.html

                bye
                TPD
                • 5. Re: Using Regex Query to replace spaces.
                  sabre150
                  Looks like I misunderstood the whole problem. Please ignore every thing I posted on this so far.

                  P.S. Don't you just need to use String.trim() or is this just homework and you have to use a regex?
                  P.P.S. I don't think look behind or look ahead are needed - just the anchors "^" and "$" with an 'or' in the regex.
                  • 6. Re: Using Regex Query to replace spaces.
                    TPD-Opitz
                    sabre150 wrote:
                    P.P.S. I don't think look behind or look ahead are needed - just the anchors "^" and "$" with an 'or' in the regex.
                    Yes, no.
                    Look behind / ahead ist overkill, <tt>trim()</tt> will do perfectly.

                    bye
                    TPD
                    • 7. Re: Using Regex Query to replace spaces.
                      AjayL
                      Hi Sabre,

                      This is a new requirement for PROD env.
                      I need to replace the leading and trailing spaces in the criteria with no spaces.
                      Thats is why i was using :
                      public String test(String criteria) {
                       
                         String regex = "^(\\s+) $";
                           
                         Pattern spacePattern = Pattern.compile(regex);
                         Matcher spaceMatcher = spacePattern.matcher(criteria);
                       
                         String resultCriteria = spaceMatcher.replaceAll("");
                       
                         return resultCriteria.toLowerCase();
                      
                      
                        Assert.assertEquals("java Regex", searchNormalizer.normalize("   JaVa        REGex   "));   // Fail
                        Output is : <1 space>java regex<1 space>
                      But this is not replacing the leading and trailing space

                      If you know the right regex to use,then help is really appreciated.

                      Thanks
                      • 8. Re: Using Regex Query to replace spaces.
                        sabre150
                        AjayL wrote:
                        This is a new requirement for PROD env.
                        I need to replace the leading and trailing spaces in the criteria with no spaces.
                        Which is exactly what String.trim() does!
                        • 9. Re: Using Regex Query to replace spaces.
                          AjayL
                          Got it.Thanks a lot.
                          • 10. Re: Using Regex Query to replace spaces.
                            AjayL
                            Sabre

                            Thanks a lot...I got the first requirement working.