Skip to main content
Known Participant
August 19, 2022
Answered

how to find last word(<=8 characters) of each paragraph?

  • August 19, 2022
  • 5 replies
  • 3181 views

I need to find last word of each paragraph. Also check lastword is <= 8 characters only( include special character, dot and numbers). Find and apply no break condition.


for example.

Common (may affect up to 1 in 10 pat):
- Severe kidney failure (this will be ascertained by your doctor through some specific blood test)
- Low levels of calcium in the blood.
Uncommon (may affect up to 1 in 100 patients):
how many mg of each tablet is in a bottle?
Each tablet contains 100 mg.
If you enter number of paragraphs or lines higher than 200 –the function will work but will produce only 200 paragraphs (10).

This topic has been closed for replies.
Correct answer Peter Spier

Peter Kahrel's code above findes the last word of a paragraph only if it contains 1 to 8 characters, but it will include closing punctuation in the count.


If you don't want to include closing punctuation in the character count of the last word I would add [[:punct:]]* which will find any punctuation if it exists (* is the zero or more times match), and maybe add \s* as well in case the original text has extraneous trailing whitespace, so the final code would look like

\s\K\S{1,8}[[:punct:]]*\s*$

 This would still fail if there is whitespace before any ending punctuation.

5 replies

Community Expert
August 22, 2022

Sorry, Eugene, you're right: what Arunkumar is after is applying no break to paragraph-final words of 8 characters or fewer and the preceding word. If that's the case, this would do:

.\s\S{1,8}$   // In a GREP style
.\\s\\S{1,8}$ // In a script

In other words, any character followed by a space followed by 8 non-space characters, at paragraph-end.

Peter Spier
Community Expert
Community Expert
August 22, 2022

Peter, I think that this should be modified with the additions I suggested above, particularly for the possibility of trailing white space unless Arunkumar25715058ufpl has already run a cleanup script to correct for that.

Community Expert
August 22, 2022

Trailing white space -- ugh. In that case, as indeed in your example, add \s* before the $

Community Expert
August 22, 2022

But, Eugene, they're looking for last single words that are fewer than 8 characters.

Community Expert
August 22, 2022

I was going by the example in the 2nd post. Which is different to last 8 characters.

Known Participant
August 22, 2022

Hi Eugene,

 

I tried your suggestion. but it is not working. Please refer the below screenshot. I am using Adobe InDesign 2020.

 

 

Finally I find this \s\H+[.\w\d\l\u]+\W*?$ - it is working but it is finds last word in all para. how to add 8 character only in this script?

 

Community Expert
August 21, 2022

This will find the last two words of a paragraph

\<(\s?(\S+)){2}$

You can then apply the no break.

 

 

Community Expert
August 21, 2022

You can then make it smarter and have digits never be on a end of line by themselves

 

 

 

Community Expert
August 20, 2022

Look for

 

\s\K\S{1,8}$

 

and apply no-break (set it in the Change format panel).

P.

Dave Creamer of IDEAS
Community Expert
Community Expert
August 22, 2022

@Peter Kahrel Wouldn't this be better as a GREP style in a paragraph style?

 

David Creamer: Community Expert (ACI and ACE 1995-2023)
Community Expert
August 22, 2022

@Dave Creamer of IDEAS -- Probably. But the question was about the GREP expression to use, and the same expression can be used in a script and in a GREP style. When you set finalised text I'd favour a script because GREP expressions always come with performance penalties. Sometimes those penalties are worth it, sometimes not.

Known Participant
August 19, 2022

I need to find and apply no breaks condition for below mentioned words.

10 pat):
blood test)
the blood.
a bottle?
100 mg.
paragraphs (10).