Using GREP to apply style to the end of paragraph

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Using GREP to apply style to the end of paragraph

Tagged: 

Viewing 9 reply threads
  • Author
    Posts
    • #116137
      Amanda Welch
      Member

      I’m certain there’s a simple answer to this, but I just can’t figure it out. I have a list of projects which all end with the project location and I want that project location to be in a different character style. I’ve been trying to figure out how to use a GREP style to search for the second comma from the end of the paragraph and apply my location character style to everything after that second comma, but I either get it to change everything after the first comma in the string or nothing at all. Does anyone have a quick solution??

      Sample Text (for all of these “Any Town, XX” should be in the location character style):

      Big Fancy Arena, Any Town, TX
      We did some work at this place, multiple garages, Any Town, FL
      Sometimes we do a lot of work, and we like to name things, with lots of commas (and even parentheses), but we like the work, Any Town, CAN

    • #116138
      Jeremy Howard
      Participant

      Hello Amanda,

      The way to accomplish this would be to utilize “\K”.

      Here is the GREP I used to make it work when testing:

      .+(?=,.+,.+$)\K.+

      The first bit — .+ matches all characters that fall before the positive lookahead

      The positive look ahead — (?=,.+,.+$) matches to a comma, 1 or more characters, another comma, 1 or more characters and then the end of the paragraph.

      Now for the really nifty part. The “\K” is used to ignore everything that has been matched by the GREP before it. That means that all of the matched characters from the first part of the GREP string — .+(?=,.+,.+$) are essentially being used to tell the second part — .+ (match 1 or more characters) where to begin.

      So the first part of the GREP string (the part before the “\K” actually matches all of the text in your paragraph up until the second to last comma, then we use \K to tell the GREP to apply the style to every character after that match.

    • #116139
      Aaron Troia
      Participant

      Hey Amanda,

      try this GREP in your paragraph style, I just tested it out so it should work.

      , \K[^,]+, \u\u\u?$

      Aaron

    • #116142
      Jeremy Howard
      Participant

      It just occurred to me that the string that I provided is matching and formatting the comma before “Any Town”. A slight tweak to the GREP fixes this:

      .+,(?=.+,.+$)\K.+

    • #116152
      Amanda Welch
      Member

      You guys are amazing! Thank you so much!!

      Jeremy, your solution worked as long as they’re wasn’t a soft return involved…but there often is. My fault for not adding that possibility to the mix.

      Aaron, your’s worked despite the soft returns.

      Victory!!

    • #116153
      Amanda Welch
      Member

      Update: slight modification.

      We also add * to the end of the paragraph to note design only projects or work with prior firms or what have you. I modified Aaron’s solution above further to…

      , \K[^,]+, .+$

      which allows anything after the last comma to still trigger the character style change.

      • #116154
        Aaron Troia
        Participant

        Perfect! I’m glad you could tweak it to fit your needs!

    • #116155
      Jeremy Howard
      Participant

      Ah yes, to make my solution work, you would need to turn multiline on with — (?m)

      Making the complete GREP string:

      (?m).+,(?=.+,.+$)\K.+

    • #116189
      Amanda Welch
      Member

      Hey guys, I was going through a final QC check today and discovered that I’m still having issues…

      , \K[^,]+, .+$ solves the forced line break issue entirely, but it’s formatting everything after the first comma in the paragraph.

      (?m).+,(?=.+,.+$)\K.+ works unless there is a forced line break just before the city name (which we often have to do).

      Ideas??

      • #116190
        Aaron Troia
        Participant

        hmmm. would this work better? this would keep it to the last two commas

        , \K[^,]+, [^,]+$

    • #116192
      Amanda Welch
      Member

      Nope. Here’s some actual text to play with including current forced line breaks…

      St. Paul’s Hospital Parkade
      Assessment and Repair, Saskatoon, SK*
      Saskatoon Health Region, St. Mary’s Villa, Structural Flooring Assessment and Truss Restoration, Humboldt, SK
      Springbank Airport Site Improvement,
      Calgary, AB
      Confidential Urban Academic
      Health Science Building, Toronto, ON
      Royal University Hospital Garage,
      Saskatoon, SK

      • #116193
        Aaron Troia
        Participant

        ohhhh the forced line breaks are defiantly making it tricky! I tried your text with some forced line breaks where I felt they probably would go and had pretty good results.

        , ?\K[^, ]+, [^, ]+(\r|\n|$)

        hmm but now it doesnt want to work on the original examples you gave :/

      • #116195
        Aaron Troia
        Participant

        well I guess taking the space out of the first set of brackets resolved it not finding anything in your original examples. so it should cover both sets of examples you gave in finding anything after the second comma.

        , ?\K[^,]+, [^, ]+(?=$|\r|\n)

    • #116196
      Amanda Welch
      Member

      YES!!!!! Thank you SO much Aaron.

Viewing 9 reply threads
  • You must be logged in to reply to this topic.
>