Skip to main content
Inspiring
August 23, 2010
Answered

Grep, Italicise Quotes

  • August 23, 2010
  • 1 reply
  • 4118 views

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

This topic has been closed for replies.
Correct answer Jongware

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:

".+?"

1 reply

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
August 23, 2010

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:

".+?"

Jongware
Community Expert
Community Expert
August 23, 2010
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:

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:

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

Marc Autret
Legend
August 24, 2010

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