Jump to content

Help me with string.gmatch


Dychuk

Recommended Posts

Hi, all!

I have string "{100}Hello, world! {45}How are you?". How I can this string split into two arrays with parameters, i.e. in the end I got array:

ar = {
	{100, "Hello, world! "},
	{45, "How are you?"}
}

I make script:

ar = {}
s = "{100}Hello, world! {45}How are you?"
for k, v in string.gmatch(s, "({%d+})(%w+)") do
	table.insert(ar, {k, v})
end

for k,v in ipairs(ar) do 
    outputChatBox("K: " .. k .. " V1: " ..v[1] .. " V2: " .. v[2]) 
end 

as a result, I received

K: 1 V1: {100} V2: Hello
K: 2 V1: {45} V2: How

but that's not what I need

but I need this

K: 1 V1: 100 V2: Hello, world! 
K: 2 V1: 45 V2: How are you?

Help me, please :(

p.s. sorry for bad english

Link to comment

maybe something like this ?

ar = {}
s = '{100}Hello, world!{end}{45}How are you?{end}'
for k, v in string.gmatch(s, '{(%d+)}(.-){end}') do
    table.insert(ar, {k, v})
end

for k,v in ipairs(ar) do 
    outputChatBox('K: ' .. k .. ' V1: ' ..v[1] .. ' V2: ' .. v[2]) 
end 

 

Edited by #,+( _xiRoc[K]; >
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...