0

I want to replace all 1.5.1234.78(25) by #1.5.1234.78(25)# this is what i do :

string ="""
sdfdfs 1.5.1234.78(25) sfsddsf
1.5.1234.78(25)
1.5.1234.78(25) ddfssfd
dsdsfdfs 1.5.1234.78(25)
1.5.1234.78(25)sdfdsf"""

print(string)
string = re.sub(r'\b{}\b'.format('[0-9]{1}\.[0-9]{1}\.[0-9]{4}\.[0-9]{2}\([0-9]{2}\)'), r'#\g<0>#', string)
print(string)

output :

sdfdfs 1.5.1234.78(25) sfsddsf
1.5.1234.78(25)
1.5.1234.78(25) ddfssfd
dsdsfdfs 1.5.1234.78(25)
#1.5.1234.78(25)#sdfdsf

why this 1.5.1234.78(25)sdfdsf is replaced by #1.5.1234.78(25)#sdfdsf

i spicify the \b to take only pattern at the beginning and end of a word !

i want have something like this :

sdfdfs #1.5.1234.78(25)# sfsddsf
#1.5.1234.78(25)#
#1.5.1234.78(25)# ddfssfd
dsdsfdfs #1.5.1234.78(25)#
1.5.1234.78(25)sdfdsf
Ram Six
  • 39
  • 4
  • You can assert a whitespace boundary at the right `\b[0-9]\.[0-9]\.[0-9]{4}\.[0-9]{2}\([0-9]{2}\)(?!\S)` https://regex101.com/r/QnTouI/1 – The fourth bird Jun 07 '21 at 07:45

0 Answers0