Skip to main content
Participating Frequently
January 19, 2013
Answered

GREP: How to target one occurrence at end of paragraph?

  • January 19, 2013
  • 2 replies
  • 2766 views

GREP newbie here. Trying to write a GREP expression that will target a photo credit at the end of a photo caption and apply a character style.

Example text:

This is the caption for a photo and there is some more caption here (at left). This is the caption for a photo and there is some more caption here. (AP Photo/John Smith)

The GREP expression needs to target the credit type inside -- and including -- the parenthesis. The trick: The expression needs to target only a single occurrence at the end of the paragraph.

Suggestions? This is what I have, which isn't working. The expression applies the style to every occurrence of type within parenthasis:

\s\(.+\)$

Any help appreciated.

This topic has been closed for replies.
Correct answer tomaxxi

Basically, it prevents picking up multiple closing parenthesis while seartching for a match.

GREP breakdown:

\([^\)]+\)$

\(    Literal opening parenthesis

[^    Exclusion: any character not in this group

    \)    Literal closing parenthesis

]    End Exclusion Group

+    While any character is NOT in this group may occur once or more times; longest possible match will be taken

\)    Literal closing parenthesis

$    End of Paragraph, Story, Footnote, or Cell

Hope that helps.

--

Marijan (tomaxxi)

http://tomaxxi.com

2 replies

tomaxxi
Inspiring
January 19, 2013

This will always match last set of parenthasis:

\([^\)]+\)$

Hope that helps.

--

Marijan (tomaxxi)

http://tomaxxi.com

Participating Frequently
January 19, 2013

Marijan --- Thanks again. This works beautifully. Now -- if I can trouble you one more time -- can you help me understand the logic?

\([^\)]+\)$

Seems like it looks for an open parenthesis, followed by either a start of paragraph or close parenthesis that appear one or more times, located at the end of a paragraph. Correct?

-- Question: why does it search for a ^ metacharcater? It works. I just don't understand why.

Participating Frequently
January 19, 2013

Basically, it prevents picking up multiple closing parenthesis while seartching for a match.

GREP breakdown:

\([^\)]+\)$

\(    Literal opening parenthesis

[^    Exclusion: any character not in this group

    \)    Literal closing parenthesis

]    End Exclusion Group

+    While any character is NOT in this group may occur once or more times; longest possible match will be taken

\)    Literal closing parenthesis

$    End of Paragraph, Story, Footnote, or Cell

Hope that helps.

--

Marijan (tomaxxi)

http://tomaxxi.com


Marijan ---

Thanks again. I have lots to learn. Very powerful.

I appreciate the help.

Jump_Over
Legend
January 19, 2013

Hi,

If could we assume a period is always before desired parenthasis (with or without space between):

(?<=\.)\s*\(.+?\)$

should work

rgds

Participating Frequently
January 19, 2013

Thanks Jump. I appreciate it. But I can't assume the period will precede the parenthesis. Could be a ? or a !