I am learning Python and Regex and I do some simple exercises. Here I have a string and I want to replace special characters with html code. The code is the following:
str= '\nAxes.hist\tPlot a histogram.\nAxes.hist2d\tMake a 2D histogram plot.\nContours\nAxes.clabel\tLabel a contour plot.\nAxes.contour\tPlot contours.'
p = re.compile('(\\t)')
p.sub('<\span>', str)
p = re.compile('(\\n)')
p.sub('<p>', str)
This code leaves the special characters (\n
and \t
) unaltered.
I have tested the regex pattern on regex101.com and it works. I can not understand why the code is not working.