There is more to formatting (disabling word wrap) than you think.
If the result is the result of a formatting operation, you must go to these rules to reverse engineer the original.
For example, you have a test
This should all be on one line
since it one sentence.
This is a new paragraph that
should be separate.
If you delete only one new line, it will look like this:
This should all be on one line since it one sentence.
This is a new paragraph thatshould be separate.
In addition, other formatting will be lost, such as intentional newlines, something like:
This is Chapter 1 Section a Section b
Included in
This is Chapter 1 Section a Section b
Finding a new line in the question is easy /(?<!\n)\n(?!\n)/
but that you replace it.
Edit : Itβs actually not so easy to find autonomous newline characters, because visually they sit among hidden (horizontal) spaces.
There are 4 ways to go.
Delete new line, save surrounding formatting
$text =~ s/(?<!\s)([^\S\n]*)\n([^\S\n]*)(?!\s)/$1$2/g;
Remove new line and formatting, replace space
$text =~ s/(?<!\s)[^\S\n]*\n[^\S\n]*(?!\s)/ /g;
Same as above, but ignores the new line at the beginning or end of the line
$text =~ s/(?<!\s)(?<!^)[^\S\n]*\n[^\S\n]*(?!$|\s)/ /g;
$text =~ s/(?<!\s)(?<!^)([^\S\n]*)\n([^\S\n]*)(?!$|\s)/$1$2/g;
An example of a regular expression breakdown (this is the minimum necessary only to isolate one new line):
(?<!\s)