4 Replies Latest reply on Jun 30, 2011 6:47 AM by tpk1982

    Attempting to create a GREP Style

    Prismatus

      I'm trying to make a GREP style for one of my paragraph styles, that will take the text from the beginning of a line until a colon and set a character style to it to bold that part.

       

      Example:

       

      Step 1: Take bread

      Step 2: Take peanut butter

      Step 3: Spread peanut butter on bread

       

      I don't think I've been getting the syntax right.  I've put ^\:~h and ^:~h into the 'to text' box, thinking this meant "Beginning of line until the character ':' and end" but neither of those have given me the results I want.  Thanks in advance for the help!

        • 1. Re: Attempting to create a GREP Style
          [Jongware] Most Valuable Participant

          Your try:

           

          ^\:~h

           

          1. You don't need to escape the colon. It's one of the few characters that don't need escaping to make them literal. (It doesn't hurt, mind you, but cleanliness etc.)

          2. You don't need the ~h  -- You'd only need that if you want to scan up to a manually "End Nested Style Here" code.

          3. You DO need to allow for a couple of characters between the start of the line and the colon. You can either use the 'any at all' wildcard (and use Shortest Match, because otherwise it will gobble up everything until the very last colon):

           

          ^.+?:

           

          or Be Literal, and only allow for "Step" and a single digit:

           

          ^Step \d:

           

          Either one will work for you.

          • 2. Re: Attempting to create a GREP Style
            Prismatus Level 1

            Your response worked for me.  I had tried using a wildcard before the ones I posted, but only put the . in, assuming it would cover more than just one character.  I assume the +? covers all characters until the colon shows up, yes?

             

            I also like the be literal part, but I was wondering what you would have instead of \d if you had more than nine steps, or if you had substeps (1a, 1b, etc.).

            • 3. Re: Attempting to create a GREP Style
              [Jongware] Most Valuable Participant

              Prismatus wrote:

               

              Your response worked for me.  I had tried using a wildcard before the ones I posted, but only put the . in, assuming it would cover more than just one character.  I assume the +? covers all characters until the colon shows up, yes?

               

              No, it's slightly more complicated than that (sorry!). By default, GREP is Greedy -- that means, if you use this

               

              ^.+:

               

              GREP will think that the Any Character wildcard may be repeated as much as possible (that's the '+') before it needs to match the colon. What this means is that it will work just as you expected for

               

              Step 1: This is a single line.

               

              but will go out of its mind with this

               

              Step 2: what will happen now? Well, contrary to what you were expecting, the entire line will be marked bold, all because everything up to the very last : will be matched!

               

              The bold bits accurately shows what happens! Another example would be this:

               

              \d+

               

              which for a string of "123" will not match just the first digit, then the second, then the third, but all of them in a single long go. By default, GREP will grab as much as it possibly can.

              Adding a question mark behind the "Repeat Me" character reverts this behavior to Non-Greedy behavior, and as such GREP will match as little as humanly possible:

               

              \d+?

               

              will then match just the "1" in "123".

               

              Prismatus wrote:

              I also like the be literal part, but I was wondering what you would have instead of \d if you had more than nine steps, or if you had substeps (1a, 1b, etc.).

               

              That's just a case of adding more specifiers. To match one or more digits, you need this:

               

              ^Step \d+:

               

              The '+', again, will allow a repetition of the "digit" code. In this case you don't have to add a question mark, because there is no way this could run out of control; first, it will only match digits, and second, these digits must be followed by a colon.

              And if you may or may not have a single lowercase character following the digit (but still before the colon), you'd use the Any Lowercase Character code and the Zero or Once repetition specifier:

               

              ^Step \d+\l?:

               

              (that's a lowercase ell.) You see the question mark meaning Something Entirely Different here? It's only a Non-Greedy marker when immediately following another repetition code, one of these

               

              * (zero or more)

              + (once or more)

              ? (zero or once)

              {4,8} (or any other set of numbers -- this is at least 4 and at most 8 times)

               

              Uh, by the way, that makes this

               

              abc??

               

              a valid GREP. It will match "ab" or "abc", and then always select the shortest possible match of these two, which is then the "ab" one. ... Uh. I'm pretty sure this may be useful, some time in the future.

              1 person found this helpful
              • 4. Re: Attempting to create a GREP Style
                tpk1982 Level 4

                Why don't you use nested styles for this one??????