Maria Gheorghe
Maria Gheorghe

Reputation: 629

Split String using lua

I need a lua code to split a youtube page line and get the youtube video title and ID .

So I need to extract using lua the id :r8ocUWwuJDg and the title : "Farsa cu camera ascunsa / Tentatiile soldatilor" form a String like the next one

</div><div class="yt-lockup-content"><h3 class="yt-lockup-title"><a href="/watch?v=r8ocUWwuJDg" class=" yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink     spf-link " data-sessionlink="itct=CMwBEJQ1GAYiEwia-4r23Z7FAhVD3xwKHSUQAKQojh4yCmctaGlnaC1yZWM" title="Farsa cu camera ascunsa / Tentatiile soldatilor" aria-describedby="description-id-58700" dir="ltr">Farsa cu camera ascunsa / Tentatiile soldatilor</a>

How can do this in lua ?How to split the text and get the title and ID? Thank you all

Upvotes: 1

Views: 193

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

In general, this kind of job should be done with a parser, not pattern matching.

Still, this should work:

local str = [[
</div><div class="yt-lockup-content"><h3 class="yt-lockup-title"><a href="/watch?v=r8ocUWwuJDg" class=" yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink     spf-link " data-sessionlink="itct=CMwBEJQ1GAYiEwia-4r23Z7FAhVD3xwKHSUQAKQojh4yCmctaGlnaC1yZWM" title="Farsa cu camera ascunsa / Tentatiile soldatilor" aria-describedby="description-id-58700" dir="ltr">Farsa cu camera ascunsa / Tentatiile soldatilor</a>
]]

local id = str:match('<a href="/watch%?v=(.-)"')
print(id)

local title = str:match('title="(.-)"')
print(title)

Note that - is used for lazy repetitions.

Upvotes: 2

Related Questions