Skip to main content
Inspiring
June 25, 2023
Question

GREP: automatically return single short word after period at end of line?

  • June 25, 2023
  • 3 replies
  • 1772 views

I'd like to know how to use GREP to identify lines that end with a period, then a space, then a word shorter than five characters, then any/no space before a line return.

 

So, if I have:

aaaa aaaaa a aaaaaa aaa aa aaaa aaaaaaaa. Cars

 

It will become:

aaaa aaaaa a aaaaaa aaa aa aaaa aaaaaaaa.
Cars

I've been searching online as well as trying to find an answer in Kahrel's GREP, to no avail...

Any help appreciated.

This topic has been closed for replies.

3 replies

pixxxelschubser
Community Expert
Community Expert
June 26, 2023
  • (manuel) line break in grep is \n
  • end of paragraph in grep is \r
turner111Author
Inspiring
June 26, 2023

Hi,

I'm referring to a natural (non-manual) line break.

Peter Spier
Community Expert
Community Expert
June 26, 2023

Not possible with GREP, nor a style.

Peter Spier
Community Expert
Community Expert
June 25, 2023

To the best of my knowledge GREP cannot identify the end of a line, only the end of a paragraph or at a forced break of some sort, so I think this would require scripting and is not something that can be added to a style. In my opinion it would really bog down the program if it were constantly re-parsing the text to see if you've made an edit that changes a line ending ad requires a new shift, which in turn may require yet another. It would be a nightmare with the paragraph composers, and only slightly better with single-line composers.

Barb Binder
Community Expert
Community Expert
June 26, 2023

Which begs the clarification:

 

@turner111 By "line break" do you mean text that appears at the end of a line or text that appears at the end of a paragraph?

 

As per @Peter Spier, you won't be able to use a regular expression to match the end of a line. Peter Kahrel's book lists the five of the seven locations on page 25, and then covers the two additional locations—\A and \z to match beginning and end of story—as you continue to work through chapter 4.

 

~Barb

 

 

~Barb at Rocky Mountain Training
turner111Author
Inspiring
June 26, 2023

Hi Barb,

Line break, excluding paragraph ends... oh well.

pixxxelschubser
Community Expert
Community Expert
June 25, 2023

Hi @turner111 

I'm not sure I understood you correctly. Do you mean something like this?

find:

\w\.\K(\h)(?=\w{1,4}\r)

replace with:

\r

 

This only works with Grep Find & Replace - not in a Grep style.

before:

 

result:

If that works for you

have fun

😉

turner111Author
Inspiring
June 25, 2023

Thanks - I'll try this out, but it needs to be something that works with a style so that the text will continually reflow.

pixxxelschubser
Community Expert
Community Expert
June 25, 2023

Sorry, in my opinion this cannot work with a Grep style.

 

But who knows?

Let's just wait and see. Other mothers have smart children too.

😉