How to add space before and after specific character using regex in Python?

PythonServer Side ProgrammingProgramming

The following code shows how space is added before and after the pipe '|' character in given string. 

Example

import re
regex = r'\b[|:]\b'
s = "abracadabra abraca|dabara | abra cadabra abra ca dabra abra ca dabra abra"
print(re.sub(regex, ' \g<0> ', s))

Output

This gives the output

abracadabra abraca | dabara | abra cadabra abra ca dabra abra ca dabra abra
raja
Published on 05-Jan-2018 06:21:09
Advertisements