Using GREP in paragraph style to format only the first occurrence of a phrase

Home Forums General InDesign Topics Using GREP in paragraph style to format only the first occurrence of a phrase

This topic contains 26 replies, has 3 voices, and was last updated by  Graham Park 1 month ago.

  • Author
    Posts
  • #100146

    John Tierney
    Member

    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

  • #100166

    Aaron Troia
    Member

    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

  • #100167

    Graham Park
    Member

    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)

  • #100168

    John Tierney
    Member

    Thanks for working through the problem for me. Looks like I am going to have to find another way of doing it.

  • #100169

    Graham Park
    Member

    If yo have a sample of the text you are using I can try to get the above method to work for you.

  • #100170

    John Tierney
    Member

    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.

  • #100171

    John Tierney
    Member

    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.

  • #100172

    Aaron Troia
    Member

    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.

  • #100177

    John Tierney
    Member

    Is there a way of posting an image into these forum discussions?

  • #100183

    Graham Park
    Member

    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?

  • #100251

    Graham Park
    Member

    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+

  • #100252

    Graham Park
    Member

    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+

  • #100259

    Aaron Troia
    Member

    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+

  • #100260

    John Tierney
    Member

    Thanks for that. I am going to try those out and I will post a picture of the result on imgur.

  • #100263

    Graham Park
    Member

    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.

  • #100264

    Graham Park
    Member

    O should have added punctuation incase you use some before the $

    ^((\w|\s|[[:punct:]])+)(\$\d\S+)

    ^((\w|\s|[[:punct:]])+)

  • #100290

    John Tierney
    Member

    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.

    https://imgur.com/a/9xjsc

  • #100291

    Graham Park
    Member

    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$

  • #100292

    Aaron Troia
    Member

    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.

  • #100299

    John Tierney
    Member

    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.

  • #100300

    John Tierney
    Member

    “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.

  • #100303

    Graham Park
    Member

    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.

    • #100304

      John Tierney
      Member

      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.

  • #100307

    Graham Park
    Member

    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
    •.+~b

    Red Price with Per
    (?=\$)\$\S+\sper\s\d+

  • #100308

    Graham Park
    Member

    Here is the text before and after the GREP listed above are applied.
    Hope this does what you are after.

    https://imgur.com/a/LalST

    • #100326

      John Tierney
      Member

      I love your Grep Ninjitsu Graham. That is exactly what I was trying to get. Thanks very much.

  • #100327

    Graham Park
    Member

    Glad to help.

You must be logged in to reply to this topic.