Miguel
Miguel

Reputation: 3054

Remove newline with re.sub

Why is the result 'bc' and not 'abc'?:

>>> import re
>>> re.sub('-\n([a-z])', '','-\nabc',re.M)
'bc'

Upvotes: 0

Views: 9765

Answers (2)

Aurielle Perlmann
Aurielle Perlmann

Reputation: 5509

You can just specify the string to be replaced:

re.sub('-\n', '','-\nabc')

will return just the abc

enter image description here

Upvotes: 1

akuiper
akuiper

Reputation: 214927

re.sub replace matched pattern with replacement string. ([a-z]) here is also matched, so it gets removed. To avoid this, you can use look ahead syntax:

import re
re.sub('-\n(?=[a-z])', '','-\nabc',re.M)
# 'abc'

Upvotes: 4

Related Questions