5 Replies Latest reply: Oct 26, 2009 7:11 AM by mattaca RSS

    GREP Find Change with Positive Lookahead not working

    mattaca Community Member

      I use GREP for find changes a lot and have built a FindChangeByList script that I use to do a lot of text styling in my documents. But I'm encountering a problem I haven't seen before.

       

      Every chapter in our cookbook has a TOC and I've asked the editors to put the words END TOC at its conclusion so I can search for the paragraphs that precede it and style them. All of the recipe variations have a tab preceding them, which made them easy to find and style:

       

       

      Find What: (?i)(?s)^\t(?=.+END TOC)

      Change To:

      Find Format: Paragraph Style = Body - text indent (this is applied to all paragraphs when I place the document)

      Change Format: Paragraph Style = TOC - Recipe Variation

       

       

      But when I search for the remaining TOC paragraphs to style them as TOC - Recipe, it can't find anything:

       

       

      Find What: (?i)(?s)^.(?=.+END TOC)

      Change To:

      Find Format: Paragraph Style = Body - text indent

      Change Format: Paragraph Style = TOC - Recipe

       

       

      Does GREP not allow you to search in Single-Line Mode when you have multiple paragraph styles taking place? You can see in the attached file that half of the paragraphs are styled and half are not. Any ideas why this is happening? And how to make it work?

       

      Matt

       

      ADDENDUM: I realized after posting this that lookaheads and lookbehinds are supposed to be fixed-length. But with some searches of this type, it DOES work! Why does it work sometimes and not others?

        • 1. Re: GREP Find Change with Positive Lookahead not working
          Tom Usrey Community Member

          You'll probably have better luck asking this in the InDesign Scripting Forum.

          • 2. Re: GREP Find Change with Positive Lookahead not working
            pkahrel Community Member

            > I realized after posting this that lookaheads and lookbehinds are supposed to be fixed-length

             

            That goes for lookbehinds. Lookaheads can deal with variable-length matches.

             

            You GREP -- (?i)(?s)^.(?=.+END TOC) -- says: "Match the first character of the story": ^. is the first character of the story, (?=.+END TOC) matches everything after the first character up to and including END TOC.

             

            What exactly are your trying to achieve?

             

            Peter

            • 3. Re: GREP Find Change with Positive Lookahead not working
              mattaca Community Member

              Thanks for the info about positive lookaheads being able to handle variable lengths, Peter. I realized my example using ^. should actually have used ^\w. Sorry about that. That still doesn't solve the initial problem though. Let me see if I can explain better.

               

              The TOCs in our cookbooks have main recipes and they also have variations. What I'm trying to do is to style ALL of them via GREP Find/Change.

               

              The attached image has three examples. The left is how the text comes to me, with all paragraphs set as Body - text indent. The middle is how it looks if I style the variations first, via the find change below. This searches for any paragraph in the document starting with a tab, before the words END TOC. Works great.

               

               

              Find What: (?i)(?s)^\t(?=.+END TOC)

              Change To:

              Find Format: Paragraph Style = Body - text indent (this is applied to all paragraphs when I place the document)

              Change Format: Paragraph Style = TOC - Recipe Variation

               

               

              The right image is how it looks if I style the main entries first, via the find change below. It searches for any paragraph starting wtih a word character, before the words END TOC. Also works great.

               

               

              Find What: (?i)(?s)^\w(?=.+END TOC) (^\w works, whereas ^. didn't)

              Change To:

              Find Format: Paragraph Style = Body - text indent

              Change Format: Paragraph Style = TOC - Recipe

               

               

              The problem is, no matter which find/change I do first, IT THEN CAN'T DO THE OTHER. My questions are:

               

              1) does having multiple paragraph styles applied prevent GREP from being able to use Single-line Mode (?s)?

              and 2) is there a way around it?

               

              I hope this makes sense.

               

              Matthew

              • 4. Re: GREP Find Change with Positive Lookahead not working
                pkahrel Community Member

                The problem is that the second query cancels the first one, no matter the order in which you apply them. This should work though:

                 

                Find What: (?i)(?s)^.+(?=END TOC)

                Change To:

                Find Format: Paragraph Style = Body - text indent

                Change Format: Paragraph Style = TOC - Recipe

                 

                Now all paragraphs up to END TOC are in "TOC - Recipe". The next step is to apply "TOC - Recipe Variation" to the paragraphs styled "TOC - Recipe" AND which start with a tab:

                 

                Find What: ^\t

                Change To:

                Find Format: Paragraph Style = TOC - Recipe

                Change Format: Paragraph Style = TOC - Recipe Variation

                 

                Does that work for you?

                 

                Peter

                • 5. Re: GREP Find Change with Positive Lookahead not working
                  mattaca Community Member

                  Brilliant! That worked perfectly! Very clever solution—wish I had thought of it.

                   

                  Thank you Peter!

                   

                  Matt