3 Replies Latest reply on Mar 7, 2016 9:54 PM by Peter Kahrel

    GREP-Positive lookbehind, or how to change only a second grouping

    carloscarmel

      First of all, forgive me if i ask questions that are easy to solve. I am a total Newbie, and altough i am trying to solve as much as i can with personal research and reading, time is running, and sometimes i just have to ask.

       

      Here is the deal: I have the following sample text:

      -------------------------------

      1. Introitus. Ps. 36, 30-31.¶

      Os justi meditábitur sapiéntiam, et lingua ejus loquétur judícium: lex Dei ejus in corde ipsíus. Ps. ibid.,1. Noli æmulári in malignántibus: neque zeláveris faciéntes iniquitátem. . Glória Patri.

      --------------------------------

      (The paragraph sign represents the hidden character for line Jump. )

      I need to make a GREP Query  that does a positive lookbehind of Introitus.+     (up until the standard carriage return ~b) and gives me as a result all the text of the following paragraph, in order to apply a style to it. An important note: almost everything in this patterns changes constantly, except the word “Introitus” and the line jumps. So far I had this:

      (?<=^Introitus.+~b)

      A positive lookbehind, that actually does not work.  I have tried adding a multiline option:

      (?m)(?<=^Introitus.+~b)

      With no results either:

      I’m sure it’s a piece of cake for you guys. Thanks in advance, and forgive the grammar.

      1. Ok. I just found out a way: Instead of doing a lookbehind, I simply divided my grep query in two:

      (^Introitus.+~b)(^.+)

      But I still need to know, how can I apply a style, only to the second result ($2), but leaving the first result intact. I don’t know how to do that. I’d still like to hear an answer, (if there is one) about how to do this with a lookbehind, (if possible.)

        • 1. Re: GREP-Positive lookbehind, or how to change only a second grouping
          Peter Kahrel Adobe Community Professional & MVP

          The problem is in your lookbehind: with the traditional lookbehind, you can't match variable-length text, and that's exactly what you're trying to do with .+ inside your lookbehind.

           

          Your example is a classic candidate for the recently discovered variable-length lookbehind (it's been with us since CS6 or so, but Adobe never told us about it):

           

          ^Introitus.+\r\K.

           

          The \K item is the variable-length lookbehind: it says "match everything before \K but don't capture it". So you match the whole line starting with Introitus up to and including the paragraph mark. Up to this point you haven't captured anything yet. That's what the dot after \K does: it captures the first character of the following paragraph (the dot after \K may be difficult to see). There's no need to capture the whole paragraph in order to apply the paragraph style, one character will do.

           

          Peter

          1 person found this helpful
          • 2. Re: GREP-Positive lookbehind, or how to change only a second grouping
            carloscarmel Level 1

            Dear Peter:

             

             

            Excellent!... I will mark your answer as correct, Altough i still would like (just out of curiosity) to know how to change only a second grouping of results, without affecting the rest, (and leaving them as they are.) Is that possible?

             

             

            Thanks a lot!

            • 3. Re: GREP-Positive lookbehind, or how to change only a second grouping
              Peter Kahrel Adobe Community Professional & MVP

              You can't apply a style to only the second of two groupings. So if you look for (.)(.) and apply a style, the style is applied to the whole match, both groupings. The only way to exclude the first grouping from the style's application is to use a lookbehind.