5 Replies Latest reply on Aug 24, 2010 6:17 AM by [Jongware]

    Grep, Italicise Quotes

    FlippinDesigner Level 1

      Hi Guys,

       

      I'm pretty new to the Grep stuff, although it seems nice and logical.

       

      I'm trying to italicise the contents of quotes within my document (am happy for the quotes to also be made italic, so no worries about being too clever trying to exclude them)

       

      The problem comes when i use:

       

      ".+"

       

      This makes all words italic even if they are between two sets of quotes i.e.

       

      "Text in quote"  Text outside Quote "Text in Second Quote" Text outside Quote "Text in Third Quote"

       

      I'm not sure how to get from that, to the following which does not italicise the text outside the quotes.  I'm assuming that i need to search for an odd number of quotes from the start of the paragraph and then only count text between that quote and the next.  Or perhaps there is a better way.

       

      "Text in quote"  Text outside Quote "Text in Second Quote" Text outside Quote "Text in Third Quote"

       

      Any help would be hugely appreciated.

       

      Cheers

        • 1. Re: Grep, Italicise Quotes
          [Jongware] Most Valuable Participant

          The problem here is that by default GREP is GREEDY. When given the chance, the operators + and * try to grab as much as they can (still without violating the other constraints you put in the expression, of course).

           

          In your case

           

          ".+"

           

          perfectly matches the entire string, from the start all the way up to the end ... But you can force GREP to use the shortest possible match by adding a single "?" after the repeat-expression. This ought to work for you:

           

          ".+?"

          • 2. Re: Grep, Italicise Quotes
            [Jongware] Most Valuable Participant
            I'm trying to italicise the contents of quotes within my document (am happy for the quotes to also be made italic, so no worries about being too clever trying to exclude them)

             

            That's actually fairly easy -- apart from one tiny little issue

            The usual way to delimit some string without including the delimiters is to use Lookbehind (?<=xx) before and Lookahead (?=yy) after the string. A typical example is

             

            (?<=\().+?(?=\))

             

            to find everything between parentheses, excluding the parentheses themselves (the open and close parentheses have to be escaped because they are 'special' inside the lookbehind/lookaheads). Similar to your previous query, it needs a shortest possible match to prevent grabbing an entire paragraph when possible.

             

            So you would think all that's needed to exclude your quotes from the string is this expression:

             

            (?<=").+?(?=")

             

            Not ... exactly. This seems to match all strings with a double quote on its left and right, but InDesign does something unexpected:

             

            quotes.PNG

             

            It matches both each opening and closing (curly) quote with the single character ", so everything between the 'between the quotes' also matches! The only thing you can do to prevent this (not really the only thing, actually) is to explicitly check for a double opening and a double closing curly quote, like this:

             

            (?<=~{).+?(?=~})

            1 person found this helpful
            • 3. Re: Grep, Italicise Quotes
              FlippinDesigner Level 1

              Awesome, i learn something every day.

               

              Who would have thought (apart from you experts) that is was just one question mark away from working.

               

              I will definitely need to learn more about Grep, and I have just the project to use it with some vengance.

               

              Cheers,

              • 4. Re: Grep, Italicise Quotes
                Marc Autret Level 4

                Hi Teunis,

                 

                By the way, there is something I don't understand.

                 

                1) The GREP pattern:

                 

                "[^"]+"
                

                 

                is OK to grab each quoted text including quotes.

                 

                BUT...

                 

                2) Using a lookbehind in the hope of retrieving the contents only:

                 

                (?<=")[^"]+
                

                 

                this does not catch nothing! (CS4, CS5).

                 

                Can anybody explain this?

                 

                @+

                Marc

                • 5. Re: Grep, Italicise Quotes
                  [Jongware] Most Valuable Participant

                  Marc, that's because the " character has been hijacked by the InDesign programmers -- as haphazard as some of their other good ideas

                   

                  The " character is nothing special in GREP, but in InDesign it can match either the straight double quote, or both open and close curly quotes. In itself both halves of your expression seem to work:

                   

                  (?<=")

                   

                  positions the cursor right after either an open or close curly quote, and this one works as expected, excluding both open and close curly quotes:

                   

                  [^"]+

                   

                  So it's only the combination that fails! Extremely irritating is that the next logical thing to try

                   

                  (?<=~{)

                   

                  also does not work -- but suddenly it comes alive when you copy a double quote from your InDesign document, and this finally works:

                   

                  (?<=“)[^"]+

                   

                  (The first dbl quote is a curly one.) (30 seconds later:) A random thing I just tried also seems to work -- and I have no clue why it does:

                   

                  (?<=["])[^"]+