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.

Known Participant
August 20, 2022

 

Hi Peter,


I tried your suggestion, but it is not working. I attached my xml, indesign file and javascript code for your reference. Also, I tried manually but it is also not working.

var doc = app.activeDocument; 


function testing(doc){
	//Clear the find/change text preferences.
	app.findGrepPreferences = NothingEnum.nothing;
	app.changeGrepPreferences = NothingEnum.nothing;

	//Set the GREP find options (adjust to taste)
	app.findChangeGrepOptions.includeFootnotes = true;
	app.findChangeGrepOptions.includeHiddenLayers = true;
	app.findChangeGrepOptions.includeLockedLayersForFind = true;
	app.findChangeGrepOptions.includeLockedStoriesForFind = true;
	app.findChangeGrepOptions.includeMasterPages = true;

	//Look for the pattern and change to
	app.findGrepPreferences.findWhat = "\s\K\S{1,8}$";
	var myFoundItems = app.activeDocument.findGrep();
	for(var n=0;n<myFoundItems.length;n++)
	{		
		var getContent = myFoundItems[n].contents;
		alert(getContent);
		app.findGrepPreferences.findWhat = getContent;
		app.changeGrepPreferences.noBreak = true;
		app.activeDocument.changeGrep();
		app.changeGrepPreferences = NothingEnum.nothing;
	}
	//Clear the find/change text preferences.
	app.findGrepPreferences = NothingEnum.nothing;
	//app.changeGrepPreferences = NothingEnum.nothing;
}

testing(doc);

 

Robert at ID-Tasker
Legend
August 20, 2022

You don't need script for that - @Peter Kahrel's reply was for use in Find&Change dialog.

 

I'm pretty sure, you need to write it a bit it differently when used in script. 

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).