16 Replies Latest reply on Oct 9, 2013 4:44 PM by Peter Spier

    GREP Styles - excluding whole words

    flaming1 Level 2

      As part of a datamerge project we were experimenting with GREP styles to see if we can get the text to change names automatically.


      We essentially have data that has come in with 'Firstname Lastname' in a few places. In just one of the occurrences we would like to strip off 'Lastname' so thought we'd experiment with a GREP style to do that. We're using a character style with a fill of none to accomplish this.


      So we have 'Dear Firstname Lastname' and want to make a GREP style that targets just 'Lastname', but leaves both 'Dear' and 'Firstname' alone. Targetting 'Dear' and 'Firstname' with a GREP style was easy. 'Dear\s\S+'finds 'Dear' followed by a space, followed by anything that is not a space until it gets to the next space. However applying the same logic to get it to exclude this hasn't been so easy.


      We tried a lookbehind to say look for 'Dear', followed by a word, and exclude them, then apply the style to the rest of the line. We started with (?<=Dear)\S+ which worked to exclude just 'Dear' and apply the style to the remainder of the line, but when we added the rest of the expression to make (?<=Dear\s\S+) we got nothing? We were able to get (?<=Dear\s\S)\D+ to work. It excluded 'Dear', plus the space and first letter of 'Lastname', but we can't seem to find any combination that will include the whole second word ('Firstname').


      It may be that there is an even simpler way to do this that simply targets the first two words in a paragraph, but we haven't been able to find any way to do that either. Perhaps we are just asking too much of GREP styles and it isn't possible... but we have come close!


      I know it is probably easier to sort this out in the data by making a new field beforehand, but we didn't have that luxury this time.


      Thanks for any pointers anyone may be able to give.