9 Replies Latest reply: Apr 24, 2009 12:48 PM by Marvin Sable RSS

    GREP Expresson - limit to whole word

    Marvin Sable Community Member

      To do a conditional search for  Cat or Cot I know part of the expression is c(a|o)t

      To find only the whole word instead of cot and cotton or cat and catnip I could use Location: beginning of word \< and   end of word  \> but suspect there is a better way?                 \<c(a|o)t\>

       

       

        • 1. Re: GREP Expresson - limit to whole word
          [Jongware] Community Member

          How could it be better than this? That's the right way to do it.

           

          OTOH, you don't have to have a selection group (x|y) -- for a single character, it's easier to type [xy] (an 'or' group). The selection group can be used in case you want to search for entire strings (or large parts of strings), like (Sun|Mon|Tues|Wednes|Thurs|Fri|Satur)day

          • 2. Re: GREP Expresson - limit to whole word
            Eugene Tyson CommunityMVP

            What are you trying to search for?

            • 3. Re: GREP Expresson - limit to whole word
              Marvin Sable Community Member

              No particular search now. I'm just trying to learn proper search methods. I see (Sun|Mon| …)----- etc is more elegant but still have issue.

               

              If I search for (Sun|Mon|Tues|Wednes|Thurs|Fri|Satur)day I will also get Sundaybest (although not a proper word it demonstrates the point. So how do I limit the find to whole word Sunday, Monday, etc.

              (cat|dog) finds cat, dog, catatonic and dogmatic
              • 4. Re: GREP Expresson - limit to whole word
                Eugene Tyson CommunityMVP

                Why not just search for

                 

                Sunday|Monday etc.?

                 

                Or if you just wanted to search for the first 3 letters with day at the end of it you could do

                 

                \<\w\w\w(?=.+day)

                 

                 

                 

                That's a Possitive Look Ahead

                (?=.+day)

                 

                So what you're saying to do is only find the first 3 letters at the begining of a word, then look at the rest of the word, if it has any character (.+) and day (.+day) up to the end of the word then only search for the first 3 letters that match that.

                 

                No other words will be found unless they end in -day like "today"

                 

                So to revise that you would do something like this

                 

                Sun|Mon|Tue|Wed|Thu|Fri|Sat|(?=\.+day)

                 

                Being more specific.

                 

                 

                But it all depends on what you're looking for.

                 

                You can find inbetween characters like say (ok this is a lame example but you get the drift)

                 

                Chapter 1: Dinosaurs – Paleontologists

                 

                and say you want "chapter 1" and "dinosaurs" to be  to be a character style so it becomes a running head but you don't want to find all Chapter 1 references, or perhaps chapters are referenced in the text so you could modify the GREP to be

                 

                Chapter \d+(?=\:)|(?<=\:\s)Dinosaurs(?=\s~=)

                 

                Then you could apply your character style because it's finding just Chapter 1 etc. but only before a Colon and it's finding only Dinosaurs after a colon but before an en dash.

                • 5. Re: GREP Expresson - limit to whole word
                  Harbs. CommunityMVP

                  I always find \b much easier to remember and read than \< and \>

                   

                  using \b to delineate word boundaries in your example would be this:

                   

                  \bc(a|o)t\b

                   

                  I wonder if there's a performance difference...

                  Harbs

                  http://www.in-tools.com

                  • 6. Re: GREP Expresson - limit to whole word
                    Marvin Sable Community Member

                    Harbs

                    Exactly what I was looking for. I needed the way to find a string (whatever it might be) that appeared by itself and not as part of a larger string. I wanted to be able to find cat but not the cat in catatonic.

                     

                    Thanks

                    Marvin

                    • 7. Re: GREP Expresson - limit to whole word
                      Marvin Sable Community Member

                      >>> and I'm getting the same result when I use Beginning of Word and End of Word as when I use Word Boundary so I''m wondering when does one know when choosing one over the other makes a difference.

                      • 8. Re: GREP Expresson - limit to whole word
                        Harbs. CommunityMVP

                        Marvin Sable wrote:

                         

                        >>> and I'm getting the same result when I use Beginning of Word and End of Word as when I use Word Boundary so I''m wondering when does one know when choosing one over the other makes a difference.

                        I don't think there's a practical difference between the two. I just find it easier to keep one code in my brain from falling out instead of two.

                         

                        Also \B is a negated word boundary so you can do something like \bcat\B which will find the first three letters of catatonic but will not find "cat"...

                        • 9. Re: GREP Expresson - limit to whole word
                          Marvin Sable Community Member

                          Right - the Capital version of the letter reverses its effect. So it will find CAT when it is Part of a word only.

                           

                          It seems like there are most often more than one way to create an expression. So if it works it right.

                           

                          Thanks again.