Merge words with hyphenation and line breaks (regex)

235 Views Asked by At

When a hyphenated word is separated by a line break, I want to merge them. So the condition is that a hyphen is followed by a line break. If a hyphen is followed by a whitespace, nothing at all should happen.

This is to be merged because a line break follows:

This is a ice-
cream and this is a car 

And in such examples, nothing at all should be merged because the line is clean.

This is a ice-cream and this is a car

How can I do this with regular expressions (I use Notepad++)?

If I use the following regular expression, then everything is just merged.

  • Search for: [^\s-]\K-\s+(?=[^\s-]) ()
  • Replace with: nothing
2

There are 2 best solutions below

0
Marty On

(?<=-)\n and replace with nothing (demo).

This is called a positive lookbehind.

0
The fourth bird On

You can use

Find what

[^\s-]-\K\R(?=[^\s-])

The pattern matches:

  • [^\s-] a non whitespace char other than -
  • -\K Match - and forget what is matched so far
  • \R Match any any unicode newline sequence
  • (?=[^\s-]) Assert a non whitespace char other than - to the right

Replace with

Leave empty

See a regex demo.

enter image description here