How would I separate a section of String using "border" symbols?

How would I separate a section of string out from say, an embedded language or something similar to that, I have tried to figure this out but to no avail.

Example: Say I had a string and it was output('Hi') and I wanted to grab and separate the content inside of the parameters of output using a function that would separate out strings by detecting 2 patterns of symbols such as ‘[{’ and ‘}]’, how would I go about doing this?

Very generally, I’ll say “character by character”, and you’ll probably want a stack.

What rules are you trying to follow?
What should the output of the parser be if I pass in:

  • output('Hello\' World')
  • output(output('foo'))
  • output('foo', 'bar')

?

2 Likes

The output should be just what is imbetween the parenthesis say if it was output(‘Hi there’) I would want ‘Hi There’.

OK now how about the three other strings I suggested as input?

local Open = "[%[%{]"
local Close = "[%]%}]"
local StringPattern = Open.."(.-)"..Close

local function SepareteString(String)
	local tab = {}
	
	for Str in string.gmatch(String, StringPattern) do
		table.insert(tab, Str)
	end

	return tab
end

local String = "Hi there {Yeah it Worked!} 5325 [25435]"
print(unpack(SepareteString(String)))


--[[
	Output:
	Yeah it Worked! 25435	
--]]```

You can use either [ or } to separete strings using this function i quickly wrote up.
2 Likes