7 Replies Latest reply: Oct 11, 2012 1:05 AM by sabre150 RSS

    Cannot find a specific pattern in text

    794113
      Hi,
      I want my code to search for a pattern in a line and then if it is there do something. The pattern may be followed by a comma, or tabs or white spaces or can have spaces before it. I am trying as follows:
      public static void testWordBoundary(){
                String text = "     /m/075g     blah blah     ";
                String pattern = "/m/075g";
                
                Pattern search = Pattern.compile("\\b"+pattern+"\\b");
                Matcher m = search.matcher(text);
                
               if(m.find()){
                    System.out.println("Found");
               }
               else{
                    System.out.println("Expression not found!");
               }
               
      }
      How can I achieve this? This above code does not work. Also, I cannot use "contains" method of string class as it says that the pattern exists if my text contains: "/m/075gzbpblahblah" as my pattern exists as a substring in the text.
      What can be a solution to this?
      Thanks
        • 1. Re: Cannot find a specific pattern in text
          965123
          Pattern search = Pattern.compile(pattern);

          Anyway, your pattern is not actually a pattern. It's a fixed String. Hence, no regex pattern are needed . If your if condition is "text.contains(pattern)", still you get a successful result.
          • 2. Re: Cannot find a specific pattern in text
            sabre150
            A couple of points -

            1) Since "/" is not a 'word' character there is no word boundary just before the "/" and you will have to be explicit about what can come just before and after your pattern. Looks to me like you need a white space "\s" or beginning of line before and white space or end of line after but of course I'm having to read between the lines.

            2) To be safe you should quote the search text either using \Q\E or Pattern.quote() .
            • 3. Re: Cannot find a specific pattern in text
              794113
              Hi
              @Kamal: Thanks for your reply but your solution will not work if second text in my example is provided. Let me know..

              @Sabre. Thanks for the reply. Actually anything can before and after the pattern, not just a white space or new line. The pattern can be at the end of the string as well: meaning there can be a carriage return after that as well. Sorry for the confusion. Probably that text was a bad example.

              Now I am trying with
              pattern.compile("\\B"+pattern+"\\b")
              which seems to work fine. Is there some issues with this new technique or is there any more sophisticated means? Will I land up in trouble with this new approach? How can this be rectified?
              • 4. Re: Cannot find a specific pattern in text
                sabre150
                kooldba wrote:
                Now I am trying with
                pattern.compile("\\B"+pattern+"\\b")
                which seems to work fine. Is there some issues with this new technique or is there any more sophisticated means? Will I land up in trouble with this new approach?
                As already explained, yes you will "land up in trouble".

                Also, only you know what the boundary conditions on the match should be but based on your original post a non-word boundary could be part of the pattern to match !
                • 5. Re: Cannot find a specific pattern in text
                  TPD-Opitz
                  kooldba wrote:
                  Actually anything can before and after the pattern, not just a white space or new line.
                  Why did you dissmiss
                  Pattern.compile(pattern)
                  ?

                  bye
                  TPD
                  • 6. Re: Cannot find a specific pattern in text
                    794113
                    @Sabre150: haha! :-) I will let you know if I am in trouble. As of now, this seems to work fine.
                    @TPD: I am keeping that option open if my pattern does not work.

                    Thanks guys!
                    • 7. Re: Cannot find a specific pattern in text
                      sabre150
                      kooldba wrote:
                      @Sabre150: haha! :-) I will let you know if I am in trouble. As of now, this seems to work fine.
                      Sorry but I wasn't meaning to be funny. You don't seem to have taken note of my first response when I indicate that you should escape the text you are looking for !