4 Replies Latest reply on Jan 29, 2013 8:27 AM by tim5319

    Help with GREP find and replace

    tim5319

      I have never used GREP before, and am stumped.  I have a document that lists the times for many different classes.  The times are variously listed as 9-10, 9- 10, 9 -10, or 9 - 10, for examples.  The problem is that there may be no space or one space on either the left or the right sides of the hyphen.  I want all the times to appear as 10:30 - 11:30, for example.  That is, the hyphen should have one space on either side of it.  I think this could be accomplished with GREP find and replace, but I don't understand the syntax well enough to make it work.

      Can someone tell me how to write the find and replace expressions?  (Of course the actual times could be any hours).

      Thanks!

        • 1. Re: Help with GREP find and replace
          Peter Spier Most Valuable Participant (Moderator)

          I think you need to do it in two steps.

           

          Search for (?<=\d)-  (that's a hypen preceded by any digit)

           

          Change to <space>$0  (the <space> is a standiin her for the ype of space you are using, so use the real thing in the dialog)

           

          then Search for -(?=\d)   (that's a hyphen followed by any digit)

           

          Chage to $0<space>

          • 2. Re: Help with GREP find and replace
            [Jongware] Most Valuable Participant

            It's tempting to add spaces to *all* numbers separated by a dash. If you are sure all times have a minutes part, then it's safer to use this:

             

            (?<=:\d\d) ?- ?(?=\d)

             

            .. replace with space dash space.

             

            It searches for "colon, any digit, any digit" (which is *found* but not *included*), followed by "one optional space, a dash, one optional space", and must be followed by "any digit" (again, not including this last one). Since the numbers before and after are *found* but not *included* -- that's due to the (?xxx) syntax -- all that needs replacing is the dash itself. By making the space before and after the dash optional (using a single "?" immediately after each), it doesn't matter if there *is* a space or not, all combinations will be found and replaced.

            • 3. Re: Help with GREP find and replace
              Peter Spier Most Valuable Participant (Moderator)

              Thank you. I'm brain dead today.

              • 4. Re: Help with GREP find and replace
                tim5319 Level 1

                Thanks for the help, everyone!