GREP: automatically return single short word after period at end of line?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
😉
Copy link to clipboard
Copied
Thanks - I'll try this out, but it needs to be something that works with a style so that the text will continually reflow.
Copy link to clipboard
Copied
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.
😉
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Barb,
Line break, excluding paragraph ends... oh well.
Copy link to clipboard
Copied
- (manuel) line break in grep is \n
- end of paragraph in grep is \r
Copy link to clipboard
Copied
Hi,
I'm referring to a natural (non-manual) line break.
Copy link to clipboard
Copied
Not possible with GREP, nor a style.
Copy link to clipboard
Copied
… Mischievously doable with a Grep style!
Char style: _JumpToNextLine
Grep expression: \.\K\h(?=\w{1,4}\h?$)
(^/) The Jedi
Copy link to clipboard
Copied
You seem to assume that each of these is a separate paragraph. OP has explicitly said this is running text and he simply doesn't want short words starting a new sentence at the right margin.
Copy link to clipboard
Copied
… Always mischievously doable with a Grep style!
Char style: _JumpToNextLine 2
Grep expression: \.\h\K(\w{1,4}\h)
(^/)
Copy link to clipboard
Copied
But what is this style doing to the format to cause that word to move? You've told us how to find the word (though not limited to end of line, it seems), but you've told us nothing about how you move it.
Copy link to clipboard
Copied
Not at all but you don't seem to read the "French" language!
"Sans retour automatique" means "No break"!
(^/)
Copy link to clipboard
Copied
typing while you were posting...
Copy link to clipboard
Copied
OK, I don't speak French, but I've found a clue in the screen shot. Your style applies No Break.
That's actually pretty clever.
Applying No Break to all words shorter than 5 characters plus the following space shouldn't hurt anything mid-line, though I question how much drag it will have on performance.
Copy link to clipboard
Copied
I CANNOT thank you enough! I have been looking for a way to do this! It works perfectly. I wanted to avoid beginning a two-letter word at the end of a line. This is brilliant.

