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

GREP Tricks: More LookBehind and LookAhead examples …

Return to Member Forum

  • Author
    Posts
    • #72064
      Allan Shearer
      Participant

      Hi Folks

      Here’s another example where I find the GREP LookBehind and LookAhead to be very helpful.

      In this case you will need a Character Style called “Italic”, and as you might expect the only attribute to turn ON is the Font Style “Italic”.

      Let’s say you have a text string pattern throughout your document where you have something like: 78 FR 12345

      Now … you DON’T know what the first two digits are. And, you DON’T know what the last 5 digits are. But, you DO know that: WHENEVER an “FR” is found AFTER 2-digits and BEFORE 5-digits, then, you want the FR to be italicised. And what’s more: you DON’T want the digits italicised.

      How would you do this?

      Simple … as long as you use “Positive LookBehind” and “Positive LookAhead” expressions in GREP.

      Here’s the GREP expression:

      (?<=\d\d )FR(?= \d\d\d\d\d)

      It’s that simple.

      Notice too that the is a ‘space’ separating the digits and the FR. Well, italicised spaces don’t look any different (at least, in THIS universe), so, YOU get to choose whether you place these INSIDE or OUTSIDE the LookBehind and LookAhead expressions. I chose ‘inside’.

      Hopefully you’ll find this helpful and I hope too that it might spark your imagination of “Where ELSE can I use GREP?!” :)

      Enjoy

      Yours,

      Allan

    • #72066
      Anonymous
      Inactive

      You can simplify that

      (?<=\d{2})FR(?=\d{5})

    • #72067
      Anonymous
      Inactive

      Sorry – (?<=\d{2} )FR(?= \d{5})

    • #72079
      Allan Shearer
      Participant

      Ah ha! I forgot to look for the solution to that. Yes, much cleaner. Thanks. :)

      Allan

    • #72142
      Aaron Troia
      Participant

      To simplify things more you could use \K (Keep), then you don’t have to type out the lookbehind syntax.

      \d{2}\KFR(?= \d{5})

    • #72148
      Allan Shearer
      Participant

      Cool! Thanks. Had never heard of \K. Thanks!

      It’s not something that is available in the UI … guess you just gotta know about it.

      Here’s what I’ve found on it, as posted in an Adobe forum:

      ————————snip————————
      (?<=pattern) \K

      A zero-width positive look-behind assertion. For example, /(?<=)\w+/ matches a word that follows a tab, without including the tab in $& . Works only for fixed-width look-behind.

      There is a special form of this construct, called \K , which causes the regex engine to “keep” everything it had matched prior to the \K and not include it in $& . This effectively provides variable-length look-behind. The use of \K inside of another look-around assertion is allowed, but the behaviour is currently not well defined.
      For various reasons \K may be significantly more efficient than the equivalent (?<=…) construct, and it is especially useful in situations where you want to efficiently remove something following something else in a string.

      For instance
      s/(foo)bar/$1/g;
      can be rewritten as the much more efficient
      s/foo\Kbar//g;
      ————————snip————————

      Allan

    • #72150
      Aaron Troia
      Participant

      No problem, it’s something I started using while working in Sublime Text while doing eBook conversions and decided one day to test if it worked in InDesign and I was surprised to see that it did, so I’ve been trying to give people a heads up, esp. with positive lookbehinds. Thank you for finding that snippet, its a more thorough than I could’ve said it myself.

      I would add on top of what they said that one difference between \K and (?<= ) is that \K is non-greedy so you can use things like \d+ and \w+ and even .+? (all with greedy quantifiers) before the \K in your regex, where as lookbehinds are greedy and so you have to be explicit in your search, you can use \d and \w, but not with greedy quantifiers (ie “+”).

      Hope I said that right.

    • #72595

      I hope you don’t mind me asking a specific GREP question here. I’m thinking that LookBehind or LookAhead may help solve the problem I’m having but I don’t know enough about GREP to get it to work correctly.

      I’m trying to figure out the correct GREP expression to modify a long list of names in the back section of our alumni magazine. I’d like to create a GREP Style within the Paragraph Style to make the names of any alumni bold. But the formatting of the names varies. Sometimes it is just a first name and last name followed by an apostrophe and their two digit graduation year, like below:

      “Elizabeth Arnold ’12 and Jeremy Raff married June 22, 2013. The couple lives in Wynnewood, Pa.”

      But sometimes it is a married female, so it would be first name, (maiden name) last name, apostrophe, two digit graduation year, like below:
      “Trina (Wolgemuth) McSorley ’86 serves on the Board of Family Promise of Cape May County.”

      And sometimes it is two alumni who’ve gotten married:
      “Corrine Stolzenburg ’09 and Clark Wenger ’09 married July 28, 2012. The couple lives in Albuquerque, N.M.”

      Is there a way to write a GREP expression that can search for two or three capitalized words preceding an apostrophe and two digits? The graduation year is the only consistent formatting that indicates an alumni.

      Here’s what I’ve done so far to try to figure it out. The section in our alumni magazine is called “class notes”, I’ve created a Character Style for “class notes bold”. And then in the Paragraph Style for class notes I’ve created a GREP style that applies “class notes bold” up to the first two digits it finds in a row (~]\d\d), this works really well since the alumni’s name is formatted at the beginning of the paragraph, but I don’t know what to do when there are two alumni in one paragraph or if another set of numbers (such as 25 years of service at their company) are listed in the paragraph it messes with the formatting.

      I’d appreciate any help you could offer!

    • #72613
      Aaron Troia
      Participant

      You might not need to search for capitalized words at all, this might work for you

      (^\“| and )\K.+?(?= ’\d+)

      of for more InDesign specific (double check the ~] as that is a single closed quotation mark and may not be the same as an apostrophe in inDesign)

      (^~{| and )\K.+?(?= ~]\d+)

      that should cover all the alumni names in your examples.

Viewing 8 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads