Match newlines that exist only after a line of text (but not between paragraphs)

77 views
Skip to first unread message

ProjectGuru

unread,
May 31, 2023, 12:00:38 PM5/31/23
to BBEdit Talk
Dear group members,  
 
I'm looking for a GREP pattern that can delete the newlines that exist ONLY after some text and NOT newline characters that exist between paragraphs For example:

=================================================

ORIGINAL TEXT:

[Ms. Rowling] This is Ms.
Rowling, author
of the Harry Potter
collection.
And now onto the show.

[intro music]

[John] Hi everyone.
Welcome back to the
Harry Potter podcast.
I'm your co-host John Doe.

DESIRED RESULT:

[Ms. Rowling] This is Ms. Rowling, author of the Harry Potter collection. And now onto the show.

[intro music]

[John] Hi everyone. Welcome back to the Harry Potter podcast. I'm your co-host John Doe.

=================================================

Have been looking around for hours to no avail.

TIA!

Neil Faiman

unread,
May 31, 2023, 12:27:27 PM5/31/23
to BBEdit Talk Mailing List
This search pattern will do the trick.

(?s)(?<=\S)\n(?=\S)


Command-F; put it in the Find: box, put a single space in the Replace: box, check Grep, and click Replace All.

Explanation:

  • (?s) — the string being searched is the entire document, not individual lines.
  • (?<=\S)) — match the search string only when it is preceded by a non-white-space character
  • \n — the actual search string: a single newline
  • (?=\S) — match the search string only when it is followed by a non-white-space character

When I apply this to your original text, I get:

[Ms. Rowling] This is Ms. Rowling, author of the Harry Potter collection. And now onto the show.


[intro music]


[John] Hi everyone. Welcome back to the Harry Potter podcast. I'm your co-host John Doe.


Cheers,
Neil Faiman

Johnny Ragadoo

unread,
Jun 1, 2023, 9:12:06 AM6/1/23
to BBEdit Talk
This appears to be what Text->Remove Line Breaks does in a single click.

But how's this for shorter and sweeter grep-ness. 

In grep mode, search for:
[^$]\n
or shorter yet:
.\n

Either one seems to find newlines at the end of text, but not on a line by themselves. Replacing with a space finishes the task.

But will Text->Remove Line Breaks do the job?

Johnny Ragadoo

unread,
Jun 1, 2023, 9:12:23 AM6/1/23
to BBEdit Talk
My post seems to have evaporated. If this turns out to be a duplicate, apologies!

Text->Remove Line Breaks appears to be a built in feature to do what you want. Basically, blocks of text with single line breaks become one long line. Two newlines separate paragraphs. View->Text Display->Soft Wrap Text will then give a pleasant visual layout without modifying the text.

In my first post I suggested some regex patterns that on second glance do not seem to work. Once again, apologies!

On Wednesday, May 31, 2023 at 11:27:27 AM UTC-5 Neil Faiman wrote:
Reply all
Reply to author
Forward
0 new messages