I want to add brackets around an expression which can contain letters or dots in a sentence.
I have tried this function :
def add_brackets(sentence, word):
s = r'(\b' + word + r'\b)'
c = re.compile(s, re.I)
return(c.sub(r'[\1]', sentence))
With
sentence = 'Le pressostat h.p. est installé'
word = 'est installé'
The output is good :
'Le pressostat h.p. [est installé]'
However with :
sentence = 'Le pressostat h.p. est installé'
word = 'pressostat h.p.'
The output is without brackets :
'Le pressostat h.p. est installé'
But with
sentence = 'Le pressostat h.p. est installé'
word = 'pressostat h.p' #no final dot
The output is with brackets :
'Le [pressostat h.p]. est installé'
The problem may be the final '.' because without it the function works. How may I include the dot in the regex expression ?