Question: I get a string line:

```
>>> line = "  abc\n  def\n\n  ghi\n  jkl"
>>> print line
  abc
  def

  ghi
  jkl

```

and I want to convert it to "abcdef\n\n ghijkl", like:

```
>>> print "  abcdef\n\n  ghijkl"
  abcdef

  ghijkl

```

I tried python re module, and write something like this: 

```
re.sub('(?P<word1>[^\n\s])\n\s*(?P<word2>[^\n\s])', '\g<word1>\g<word2>', line)

```

but I get this:

```
>>> re.sub('(?P<word1>[^\n\s])\n\s*(?P<word2>[^\n\s])', '\g<word1>\g<word2>', line)
Out: '  abcdefghijkl'

```

It seems to me that the `\n\s*` part is also matching `\n\n`. Can any one point out where I get it wrong?

Answer: 
`\s` matches space, `\t`, `\n` (and, depending on your regex engine) a few other whitespace characters.

So if you only want to replace single linebreaks + spaces/tabs, you can use this:

```
newline = re.sub(r"(?<!\n)\n[ \t]*(?!\n)", "", line)

```

**Explanation:**

```
(?<!\n) # Assert that the previous character isn't a newline
\n      # Match a newline
[ \t]*  # Match any number of spaces/tabs
(?!\n)  # Assert that the next character isn't a newline

```

In Python:

```
>>> line = "  abc\n  def\n\n  ghi\n  jkl"
>>> newline = re.sub(r"(?<!\n)\n[ \t]*(?!\n)", "", line)
>>> print newline
  abcdef

  ghijkl

```
Try this,

```
line = "  abc\n  def\n\n  ghi\n  jkl"
print re.sub(r'\n(?!\n)\s*', '', line)

```

It gives,

> 
>  abcdef  
> 
>  ghijkl
> 
> 
> 

It says, "Replace a new line, followed by a space that is NOT a new line with nothing."

UPDATE: Here's a better version

```
>>>  re.sub(r'([^\n])\n(?!\n)\s*', r'\1', line)
'  abcdef\n\n  ghijkl'

```

It gives exactly what you said in the first post.