4 Replies Latest reply: Apr 10, 2009 3:49 PM by Marvin Sable RSS

    Example of Shortest Match & Longest Match

    Marvin Sable Community Member

      In GREPeze I am "basically" understanding the concept of longest match vs shortest match but can't come up with an example (that works) to demonstrate the difference. Also, is longest / shortest relevant to only a category of search criteria or all searches.

       

      Thanks

      Marvin

        • 1. Re: Example of Shortest Match & Longest Match
          Eric @ MCA Community Member

          Say you had some text that said "Please see pp. 45, 50 and 90 for more info."

           

          Searching for pp.*[0-9]+ would match "pp. 45, 50 and 90" because it extends the ".*" as far as it can with a matching number at the end.

           

          However, searching for pp.*?[0-9]+ would only match "pp. 45" because it stops at the shortest match.

           

          Is that what you mean?

           

          It also makes a huuuuge difference if you are trying to do grep replacement within HTML code, because if you try and search to the next closing angle bracket and forget the less greedy "?" symbol, you will match the rest of the document!

          • 2. Re: Example of Shortest Match & Longest Match
            Marvin Sable Community Member

            Yes. So the way the find what is expressed can determine if shortest 

            match is needed? In other words could you achieve the same "find" in 

            your example with a different search expression without the need for 

            sing shortest match?

             

            If I do this search   \d+

            on this text          12345 word 45832 another word  667

             

            I do not need shortest match

             

            But now I'm seeing that if I add a decimal   123.45 word 458.32 

            another word  6.67

            then the decimal interrupts the match

             

            so do I create the need for shortest match when I change the 

            expression to include the figures on wither side of the decimal?

            • 3. Re: Example of Shortest Match & Longest Match
              Eric @ MCA Community Member

              Basically, whenever you start using wildcard expressions, you will need to start worrying about the match length (or whether they are "greedy" matching, which they are by default).

               

              .* is the big one to watch for.

               

              BTW, in your example, you could search for numbers with decimals or commas by adding that to your character set.

               

              [\d.,]+ would not need to worry about greed because it will stop the first time it doesn't see a number, comma or decimal point.

               

              EDIT: And in my example, I could do a search for exactly pp. \d+ to match the pp and only the first number.

              • 4. Re: Example of Shortest Match & Longest Match
                Marvin Sable Community Member

                Got it. You turned the light on for me. I was thinking  to find 678.98 

                I needed to search for a digit 1 or more times/ then a character one 

                or more times/ then a digit one or more times.  That explains the need 

                for shortest match.

                 

                Thanks

                Marvin