2

I have a wall of text (no code, numbers or anything like that). The sentences are sometimes split in the "middle" by a "paragraph" or "manual line break".

For example:

The most obvious part was a hollow, echoing quiet, made by things that
were lacking. If there had been a wind it would have sighed through the
trees, set the inn’s sign creaking on its hooks, and brushed the silence
down the road like trailing autumn leaves. If there had been a crowd,
even a handful of men inside the inn, they would have filled the silence
with conversation and laughter, the clatter and clamor one expects from
a drinking house during the dark hours of night. If there had been music
... but no, of course there was no music. In fact there were none of
these things, and so the silence remained.

I would like to create a regex or extended replace where I could stitch these lines together.

I used to do this in microsoft word, where I've searched for "line breaks" where it had an alphabet character before it (instead of a dot (.) or an exclamation mark (!), question mark (?) or quotation marks (") and replaced them with nothing.

I don't use microsft word anymore, and although open office can do it, it is a really-really-really slow process.

Is there a way to do this in notepad++, with regex or extended replace?

2

Using a regex, you can do the following:

Find what : ([^.!?"])\r\n

Replace : $1 (a trailing space needs to be added, but doesn't show up when rendered with Markdown)

Explanation

([^.!?"]) will group any character that is not ., !, ? or ". If you actually wanted it to allow any alphanumeric character, you could change this to ([\w]), which will match any character from A to Z, either upper or lower case, any number, or an underscore (_). If you're really set on only allowing A to Z, you could use ([A-Za-z]).

The square brackets are used to essentially say "allow/disallow the following characters" (disallow is specified by adding the ^ character just after the opening square bracket). Square brackets also allow the above syntax of A-Z which, as you can expect, allows anything between the two provided values.

Finally, by using the parentheses around the grouping (which applies to all variations provided above), this value can be placed back into the string using the $1 placeholder, which is discussed further down.

\r\n matches the line break in Notepad++ (in other editors, a line break may be \n or \r instead).

Altogether, the two sections of the regex itself make sure to match only a character that you've specified, followed by a line break.

The $1 replace statement (again, making sure to add a space afterwards) will take what we matched in the regex itself (the last character on that line that is followed by a line break) and place it back on the line (this is so that we don't remove the character as would otherwise happen without this statement). The space is simply there to make sure that when the lines are pulled together, the words are still distinct and don't merge together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.