I have a certain string in my row of my text file, such as this:

```
<img title="\frac{3}{8}" src="http://latex.codecogs.com/gif.latex?\dpi{50}&amp;space;\fn_phv&amp;space;\frac{3}{8}" alt="" />

```

Basically it contains a html code for a fraction. But now I would like to replace it with:

```
<sup>3</sup>&frasl;<sub>8</sub>

```

So it should take the word "\frac{3}{8}" and replace it accordingly for each line. The word "\frac{3}{8}" will also change accordingly.

I know I need to use regular expressions but need guidance to do this.

My code:

```
with open('/Users/cnnlakshmen/Downloads/qz_question.sql', 'r') as fin:
    filedata = fin.read()

filedata1 = re.sub(r'<img\b[^<]*(?<=title=")\\frac\{(\d+)\}\{(\d+)\}"[^<]*>', "<sup>\g<1></sup>&frasl;<sub>\g<2></sub>", filedata)
print filedata1

# Write the file out again
with open('/Users/cnnlakshmen/Downloads/qz_question1.sql', 'w') as fin:
  fin.write(filedata1)

```