1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You great ! what is '%' for ? In str:match('<a href="/watch%?v=(.-)"') . I tried string.match( line, "watch?v=(.-)\"") and I received errors without '%' before the '?' . What does '%' ?
@MariaGheorghe ? is a magic character in Lua patterns, it must be escaped with % to match the character ? itself. See Patterns.
Can you please look at this question too? stackoverflow.com/questions/29979650/close-lua-state-and-reopen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.