Get string between strings

Hello,

I have a string (below) that I need to extract a string from using the strings before and after it.

<title>hello world</title>

Example:

I need to get the text between <title> and </title>
so that it would return hello world.
How would one do that?

You can use a pattern like '<title>(.*)</title>' and match it with a string.

local String = '<title>hello world</title>'

local res = string.match(String, '<title>(.*)</title>')

print(res)--hello world

Note & Edit: The * modifier will match 0 or more times, so naturally inputting '<title></title>' will return a blank string ('') instead of nil. if a nil result is desired you can use the + modifier.

8 Likes