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

InDesign GREP – Simplifying an Expression

Return to Member Forum

  • Author
    Posts
    • #1179127
      John Penney
      Participant

      I am struggling to learn GREP in InDesign and I purchased the book “GREP for InDesign” by Peter Kahrel. Every time I think I learn something, when I try it, it doesn’t work. I would like assistance with an expression I have been struggling with.

      I typeset a lot of publications that list different months of the year followed by a date and I want to apply a “NO BREAK” character style to word spaces between the two. I would also like an expression that finds the months of the year even if they are abbreviated (I haven’t figured that out yet). Below is the expression I have been using, but I think it needs to be simplified for efficiency (with a marking subexpression?).

      (?<=January) (?=\d)|(?<=February) (?=\d)|(?<=March) (?=\d)|(?<=April) (?=\d)|(?<=May) (?=\d)|(?<=June) (?=\d)|(?<=July) (?=\d)|(?<=August) (?=\d)|(?<=September) (?=\d)|(?<=October) (?=\d)|(?<=November) (?=\d)|(?<=December) (?=\d)

      As you can tell, the basic structure I put together is using Positive Lookbehind for each month and Positive Lookahead for the digit. I have been using the vertical backslash (pipe) so it it will find one OR the next OR the next, and so on. The sequence repeats itself for each month and date. I came across an article that recommended limiting the number of parenenthesis in expressions. This might explain why some documents seem run slowly, but I am not sure.

      Any assistance you can provide would be greatly appreciated. Thank you.

    • #14324356
      Aaron Troia
      Participant

      Hey John, Try this,

      (Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|June|July|Aug(ust)?|Sept(ember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?).?\K (?=\d+)

      Aaron

    • #14324355
      John Penney
      Participant

      Thank you Aaron for the the quick response.

      I am not sure if I am doing something wrong or not, but I ran a test on a paragraph of text from a newsletter and the expression did not apply the “no break” character style to the month and date. I then created a character style that would apply a highlight color to the characters that the expression was intended to find (for visibility purposes) and it also did not apply the style. I tried your expression using the GREP find / replace and with a GREP style. Thank you in advance.

    • #14324354
      Aaron Troia
      Participant

      ohhhh ok, I see what you are wanting to do. Your expression and my previous expression are ment to *only* find the space between the month and the date but to also ignore the month and the day. That’s what Lookahead, lookbehind, Keep (\K) do, they find what you put in them before and after what you are wanting to change, but at the same time they also exclude them from the search, so they are referred to zero length assertions.

      This is an easy fix, all we have to do is remove those zero length assertions. So try this now, which should work for what you want to do, which is applying a character style to the month and day.

      (Jan.?(uary)?|Feb.?(ruary)?|Mar.?(ch)?|Apr.?(il)?|May|June|July|Aug.?(ust)?|Sept.?(ember)?|Oct.?(ober)?|Nov.?(ember)?|Dec.?(ember)?) (\d+)

    • #14324353
      David Blatner
      Keymaster

      John: Your original expression won’t work because you can’t have a whole bunch of “positive lookbehind” in a row.
      Aaron’s suggestion is interesting, but note that it literally only finds the space between the month and the date. If you apply a No Break to that (with a grep style, for example) then it won’t break.

    • #14324352
      Aaron Troia
      Participant

      David is correct. I didnt think of if that way. Also, if you run the first expression as a GREP find and replace the space with non-breaking space character throughout, it would also work.

    • #14324351
      John Penney
      Participant

      Aaron, This second expression you posted works beautifully. It applies “no break” to the month and date; even if it is abbreviated.

    • #14324349
      Aaron Troia
      Participant

      John,

      Good I’m glad that worked for you! From what you have learned in GREP so far, does the expression make sense?

    • #14324348
      John Penney
      Participant

      Well, not exactly. From my limited knowledge, you removed the positive lookbehind and positive lookahead because it was very repetitive and inefficient. Unlike my expression that only applied the “no break” to the space between the month and date, your expression applies “no break’ to the month AND the year. If you are offering a more complete explanation, that would be great as I am thinking I can modify this for other uses.

      Thank you so much!!

Viewing 8 reply threads
  • You must be logged in to reply to this topic.
Forum Ads