Replace text inside tag

2

I'm trying to replace within a tag? EX:

mystr = '<html><head></head><body><p>Aqui meu texto</p></body></html>'
mystr = mystr:gsub('<.+>', '')
print(mystr)

I would like it to return:

Aqui meu texto

But it does not return anything.

    
asked by anonymous 13.01.2015 / 14:07

1 answer

4

The problem is that '<.+>' houses all the string and gsub then removes all content.

If you want to extract text, use string.match and captures:

print(mystr:match('<p>(.-)</p>'))

If you want to replace the text, use

mystr = mystr:gsub('<p>.-</p>','<p>Eis o novo texto</p>',1)
    
13.01.2015 / 14:43