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: GREP
- This topic has 13 replies, 3 voices, and was last updated 5 years ago by Amanda Welch.
-
AuthorPosts
-
-
April 17, 2019 at 8:41 am #116137Amanda WelchMember
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 -
April 17, 2019 at 8:53 am #116138Jeremy HowardParticipant
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.
-
April 17, 2019 at 8:54 am #116139Aaron TroiaParticipant
Hey Amanda,
try this GREP in your paragraph style, I just tested it out so it should work.
, \K[^,]+, \u\u\u?$
Aaron
-
April 17, 2019 at 9:01 am #116142Jeremy HowardParticipant
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.+
-
April 17, 2019 at 11:43 am #116152Amanda WelchMember
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!!
-
April 17, 2019 at 11:47 am #116153Amanda WelchMember
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.
-
April 17, 2019 at 12:10 pm #116154Aaron TroiaParticipant
Perfect! I’m glad you could tweak it to fit your needs!
-
-
April 17, 2019 at 12:44 pm #116155Jeremy HowardParticipant
Ah yes, to make my solution work, you would need to turn multiline on with — (?m)
Making the complete GREP string:
(?m).+,(?=.+,.+$)\K.+
-
April 18, 2019 at 12:05 pm #116189Amanda WelchMember
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??
-
April 18, 2019 at 12:51 pm #116190Aaron TroiaParticipant
hmmm. would this work better? this would keep it to the last two commas
, \K[^,]+, [^,]+$
-
-
April 18, 2019 at 1:05 pm #116192Amanda WelchMember
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-
April 18, 2019 at 1:46 pm #116193Aaron TroiaParticipant
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 :/
-
April 18, 2019 at 2:08 pm #116195Aaron TroiaParticipant
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)
-
-
April 18, 2019 at 2:43 pm #116196Amanda WelchMember
YES!!!!! Thank you SO much Aaron.
-
-
AuthorPosts
- You must be logged in to reply to this topic.