Skip to main content
Mike Witherell
Community Expert
Community Expert
February 2, 2017
Answered

GREP solution last word?

  • February 2, 2017
  • 1 reply
  • 2822 views

Hi Gang,

How would you make a GREP to find the last word in the paragraph in order to italicize it?

Best,

Mike in Washington DC

    Correct answer Peter Kahrel

    [-\w]+[[:punct:]]+?$

    i.e. string of word characters or hyphen, maybe followed by punctuation, at the end of a paragraph ($)

    Or [-\w]+(?=[[:punct:]]+?$) if you want to capture just the word, not the end punctuation.

    Peter

    1 reply

    Peter Kahrel
    Community Expert
    Peter KahrelCommunity ExpertCorrect answer
    Community Expert
    February 2, 2017

    [-\w]+[[:punct:]]+?$

    i.e. string of word characters or hyphen, maybe followed by punctuation, at the end of a paragraph ($)

    Or [-\w]+(?=[[:punct:]]+?$) if you want to capture just the word, not the end punctuation.

    Peter

    Mike Witherell
    Community Expert
    Community Expert
    February 2, 2017

    Thanks Peter:

    It works! Mostly. It does not like the final paragraph, though, which closes with a double quote.

    Tell me, Peter, what does the initial - sign mean?

    Mike Witherell
    Obi-wan Kenobi
    Legend
    February 2, 2017

    [-\w] stands for 'hyphen or word character'.


    Hi Peter,

    Another approaches:

    \H+[-\w]+\W*?$ (Green)

    [-\w]+(?=\W*?$)  (Orange)

    These codes "seem" to catch the "last word" taking in account its context (before/after, except space, taken or not taken)!

    (^/)