Using GREP in paragraph style to format only the first occurrence of a phrase
Learn / Forums / General InDesign Topics / Using GREP in paragraph style to format only the first occurrence of a phrase
- This topic has 26 replies, 3 voices, and was last updated 6 years, 3 months ago by Graham Park.
-
AuthorPosts
-
-
December 7, 2017 at 4:31 pm #100146John TierneyMember
I am trying to apply a character style to a price in a paragraph. However, there are instances where a second price occurs in the same paragraph and I don’t want the style to apply to that second instance. This is an example of the sort of thing I might be formatting.
Product Name $9.99 which is $9.99 per kilo
Product Name $7.88 per kilo
Product Name $6.50 this is $3.25 ea
I am using \$[\d|\.]+ to select the price but it selects every instance in each paragraph.
I have tried using shortest match with \$[\d|\.]+?\s but that is clearly not right.
What am I missing?
Thanks
-
December 7, 2017 at 5:49 pm #100166Aaron TroiaParticipant
John,
That is a good question, I tried using the ^ to signify the start of the line to get more specific, but it seems GREP Styles does not like that, I can either find the first $9.99 or all of them, there’s no middle ground. Its like they make it so you cant get too specific. I might ditch the GREP Style and just do a GREP search, I tried it with the RegEX below and it worked fine, but it wouldnt work as a GREP Style. Weird.
Fine:
^[^$]+\K\$[^ ]+
Replace:$0
Sorry it’s not exactly what you were asking for, but that should work.
Aaron
-
December 7, 2017 at 7:34 pm #100167Graham ParkMember
Is there a logic to the second instance of the price?
If there is you can create a second GREPEg
First – set the format for the first number
(\$\S+)
Then – set this one to the NORMAL format so this over rides the above format
(\$\S+ ea) -
December 7, 2017 at 7:54 pm #100168John TierneyMember
Thanks for working through the problem for me. Looks like I am going to have to find another way of doing it.
-
December 7, 2017 at 8:01 pm #100169Graham ParkMember
If yo have a sample of the text you are using I can try to get the above method to work for you.
-
December 7, 2017 at 8:03 pm #100170John TierneyMember
Is there a logic to the second instance of the price?
If there is you can create a second GREP
Eg
First – set the format for the first number
(\$\S+)
Then – set this one to the NORMAL format so this over rides the above format
(\$\S+ ea)Thanks for that suggestion Graham.
That is how I am getting around the problem at the moment but it just seems like an inelegant solution to me. I just thought there would be an easier way of doing it and that I must have been missing something that a GREP egg head would see straight away. There is no guarantee that there will always be a logic to the second price instance so there is the possibility that something could go wrong.
-
December 7, 2017 at 8:59 pm #100171John TierneyMember
Is there a way of using a positive look behind to find the beginning of a paragraph and then any letter or space character in the text preceding the first price? The second price would have space characters, letter characters and digit characters preceding it. The first appearance of the “$” will never have digits preceding it. I am not sure how to write it. I have tried:
(?<=^[\u\l.]+) to find any letter or space after the beginning of the paragraph and before the first “$”.
I end up with
(?<=^[\u\l.]+)(\$[\d\.]+) which I thought would find only the first price of any paragraph.
But it doesn’t seem to work.
-
December 7, 2017 at 9:30 pm #100172Aaron TroiaParticipant
No that wont work, lookbehinds must be non-greedy (no +), I tried using Keep (\K) which acts like a positive lookbehind but allows you to be greedy, but the problem with the GREP styles is that for some reason it doesnt like the beginning of line character. As soon as I would add that into my RegEx, my GREP style wouldnt act correct, but it would work fine in a GREP search.
-
December 7, 2017 at 9:45 pm #100177John TierneyMember
Is there a way of posting an image into these forum discussions?
-
December 7, 2017 at 10:05 pm #100183Graham ParkMember
Use https://imgur.com/ and place a link in your post.
Is there a pattern to the second price that is different to the first that we could use in the GREP? -
December 10, 2017 at 5:05 pm #100251Graham ParkMember
Just a thought, is the second price always preceded by ‘is’ if so then create a GREP in your paragraph style
This will find the is followed by a $ and everything up to the next white space.(?<=is)\s\$\S+
-
December 10, 2017 at 5:18 pm #100252Graham ParkMember
Sorry I should be more precise.
Two GREP in your paragraph style, the first one will add your text style to all $prices, the second one will replace the format with the normal character style.First
Apply Style This is the style you want for the price.
To Test \$\S+Second
Apply Style: Normal, create a style that has the format of this paragraph text
To Text (?<=is\s)\$\S+ -
December 11, 2017 at 12:05 pm #100259Aaron TroiaParticipant
Graham made a good observation, but you don’t need two GREP styles, one will do. Using Graham’s GREP and modifying it slightly, this should work,
(?<!is)\s\$\S+
-
December 11, 2017 at 3:42 pm #100260John TierneyMember
Thanks for that. I am going to try those out and I will post a picture of the result on imgur.
-
December 11, 2017 at 10:23 pm #100263Graham ParkMember
How about this, it does not rely on the ‘IS’ to work and such will work with any text.
Format for the number
^((\w|\s)+)(\$\S+)
Then to add normal format back to the beginning of the paragraph
^((\w|\s)+)Maybe Aaron can tweak this into one expression as well.
-
December 12, 2017 at 12:28 am #100264Graham ParkMember
O should have added punctuation incase you use some before the $
^((\w|\s|[[:punct:]])+)(\$\d\S+)
^((\w|\s|[[:punct:]])+)
-
December 12, 2017 at 4:07 pm #100290John TierneyMember
I have tried those suggestions. They didn’t seem to work for me.
However, here is a link to an Imgur post that lays out the problem.
-
December 12, 2017 at 4:57 pm #100291Graham ParkMember
This example is not the same as your original request.
3 GREP to make it work, but the text needs to follow the same pattern.Character format Bold Black for leading text
^((\w|\s|[[:punct:]])+)(\$\d\S+)Character format Red Prices, if these are not all ‘per’ then a list would be needed to make this work
\$\d\S+ (per\s\d+)?Character format Normal (to correct the last price)
\S+ ea$ -
December 12, 2017 at 5:04 pm #100292Aaron TroiaParticipant
Darn, sorry those didnt work John.
Looking at the image you posted, is this how each product description is formatted or does it vary? meaning, is the last price always followed by “ea.” or does that change? Just trying to get more of an idea of what is a constant in the descriptions that we can put into the RegEx to help you out. I could probably post one now (
\$\S+ (?!ea.)
) that might fit the image that you posted but it might not work in every instance if it changes. -
December 12, 2017 at 6:37 pm #100299John TierneyMember
That is the thing. That last price with the “ea” after it might be there, it might not. The “ea” might be there, it might not. I am trying to make the paragraph style and the GREP in particular as flexible as possible. Ideally being able limit the GREP style to the first occurrence of the “$” would be great.
-
December 12, 2017 at 6:42 pm #100300John TierneyMember
“This example is not the same as your original request.”
Yes Graham, you are right, the example isn’t the same as my first post but I just tried to keep it as simple as possible. I thought there would be some obvious way of finding only the first price in a paragraph.
-
December 12, 2017 at 7:27 pm #100303Graham ParkMember
Well sort of but you seem to be formatting the second price instance as well.
This is easy to achieve with nested styles just add the END NESTED STYLES mark when you want the format to change but this is not automated.
Try my three GREP above and it will bold the description, make the prices red (including the per 100) and then make the last price normal again,
With GREP you need to be very specific about the text you are formatting as it matched patterns so we need to see some good sample text to get the GREP statements correct. Two or three prices can make a big difference to how to approach the task.-
December 12, 2017 at 8:35 pm #100304John TierneyMember
Thanks for your help but still doesn’t seem to be working. I tried those three GREP ideas but they don’t cover every instance. You see sometime it will say something like:
Product name and description $12.00 • Made from a durable unobtanium™ Metal Frame® • Lightweight and folds flat for easy storage • choose from jute or black polyester bag $120.00 per 10 • Made in China. $12.00 ea.
Sometimes:
Product name and description $12.00 • Made from a durable unobtanium™ Metal Frame® • Lightweight and folds flat for easy storage. $12.00.
There may be many different ways for a price to appear within the body of the paragraph after the first bullet point.
EDIT: And, yes, you are right of course about the formatting of the second price. That is exactly the point. I want to be able to format the second price if I want to but just leave it unformatted if I choose to. I want the second price to only format if it has the word “per” as part of the price string. Getting that bit was easy the problem was that it was formatting even without the word “per” in it. The price always formats just like it does in the first instance. I thought it would have been an easy thing to work out but clearly not.
-
-
December 12, 2017 at 9:44 pm #100307Graham ParkMember
With your text this works for me, they have to be in order
Bold Black
^((\w|\s|[[:punct:]])+)(\$\d\S+)Red Price
\$\d\S+ (per\s\d+)?Normal
•.+~bRed Price with Per
(?=\$)\$\S+\sper\s\d+ -
December 13, 2017 at 12:31 am #100308Graham ParkMember
Here is the text before and after the GREP listed above are applied.
Hope this does what you are after.-
December 13, 2017 at 7:12 pm #100326John TierneyMember
I love your Grep Ninjitsu Graham. That is exactly what I was trying to get. Thanks very much.
-
-
December 13, 2017 at 8:06 pm #100327Graham ParkMember
Glad to help.
-
-
AuthorPosts
- You must be logged in to reply to this topic.