Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

GREP style that selects the last instance in a paragraph

Return to Member Forum

  • Author
    Posts
    • #14347042
      theobear
      Member

      I’m trying to format the attribution of a quote that is inside parentheses after the quote… the trouble is sometimes there are parentheses inside the quote.

      So:
      This is a quote from a famous writer (and sometimes the writer uses parentheses) but the attribution is also in parens

      is there any way to select the last instance of a set of parentheses and everything inside?

      This is what I used: \(.+\)$

      but it selects

      “(and sometimes the writer uses parentheses) but the attribution is also in parens. (Attribution)”

    • #14347049
      David Blatner
      Keymaster

      Can you provide a couple good examples of things you are trying to format?

      • #14347053
        theobear
        Member

        Thank you David and Dhafir, Dhafir, That worked! can you tell me what exactly that code is doing?
        Poking around some old files (it’s funny how you sometimes solve the same problems over and over. :) I found this solution. I have no idea why, but this one works too:

        \([^)]+\)$

        if you know what that is doing I would be interested to know why this one works too.

        Thanks again!

      • #14347054
        Dhafir Photo
        Participant

        Hello! Your regex finds set of parentheses only in the end of paragraph, but my regex finds find the last set in anywhere

        \([^)]+\)$
        \( open parentheses
        [^)] any character except the close parentheses (this form is called negated character set)
        + one time and more (but you should use here +? to make it shortest match)
        \) close parentheses
        $ end of paragraph

        .*\K\(.+?\)
        . any character
        * zero or more times
        \K keep all the before outside the matching
        \(.+?\) the same above

      • #14347055
        theobear
        Member

        Thank you so much!

    • #14347052
      Dhafir Photo
      Participant

      To find every last set of parentheses use this: .*\K\(.+?\)

    • #14347056
      Nick B
      Participant

      You already have your answer on how to achieve the selection, but in case you wanted to know why your original suggestion didn’t work, the “.+” will include any intervening opening or closing parentheses, so it:

      – finds the first “(”
      – matches “.+” (including any parentheses)
      – matches the “)” at the end per the rule

      If there was no closing parenthesis then obviously even though it matched the first two parts, the expression would return nothing. So as others have pointed out, you just have to make sure the middle rule is matching what you want, i.e. no opening parentheses, in which case when it finds the first “(” it will not match the expression but when it gets to the last “(” it will.

      • #14347070
        theobear
        Member

        thank you for the clear explanation!

Viewing 3 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads