L
L
LoliNeko2014-11-19 06:28:38
Lua
LoliNeko, 2014-11-19 06:28:38

How to get content between tags in a string using string.find?

--получаем позиции содержимого в строке вместе с тегами
i, j = string.find (data, "<body>(.-)</body>")
--отнимает количество символов эквивалентных размерам тегов
msg = string.sub (data, i + 6, j - 7)

How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Lerg, 2014-11-19
@LoliNeko

Like this?
repl.it/408/2

local data = 'asdds d sf fsadsadsad<body onload="javascrip:()">the text inside the tag</body>asdsadas ds fsd sdf dsadsad'

local tag = 'body'

local tagStart, tagEnd = data:find('<' .. tag .. '.->')
local closeTagStart, closeTagEnd = data:find('</' .. tag .. '.->', tagEnd + 1)

local textStart, textEnd = tagEnd + 1, closeTagStart - 1

local msg = data:sub(textStart, textEnd)
print(msg)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question