How to properly split up a string

Hello, I have been trying to make more expressive text (like a wavy text effect) for my dialog system, but I got stuck into splitting the text up.

local text = '<effect=wave>Hello!<wave=/> How are <color="#FF0000">you?<color=/>'

local t = {}

string.gsub(text, "<(.-)>([%w%p]+)", function(word,text) table.insert(t, word) table.insert(t, text) end)

for i,v in t do
      print(i,v)
end

what I have here is a piece of text that I want to split up
what I want to get is something like this:

<effect=wave>
Hello!
<wave=/>
How are
<color="#FF0000">
you?
<color=/>
but i am getting something like this
image

Thanks

local String = '<effect=wave>Hello!<wave=/> How are <color="#FF0000">you?<color=/>'

for Sub in string.gmatch(String, "<?[^<>]+>?") do
	print(Sub)
end

image

1 Like

Thank you so much, mate. Could you go through the partern in detail? .I would like to learn more about them

Of course.

<?[^<>]+>?

The ? modifier matches 0 or 1 occurrence(s) of the previous character class, in this case that is either β€˜<’ or β€˜>’, the character set β€˜[^<>]+’ matches 1 or more occurrence(s) of any character which isn’t either β€˜<’ nor β€˜>’.

1 Like