• Chat command parsing with patterns
    3 replies, posted
I'm having some problems with this, everything else works, but parsing quotes like Source does is killing me. I know I could do this just by using player:ConCommand but I prefer doing stuff from scratch. This is what I have [lua] local str = '!this is "random shite" and "another arg"' local cmd = str:gsub( "^[!/](%S+)(.*)", "%1" ) -- this str = str:gsub( "^[!/](%S+)%s*", "" ) -- is a test "random shite" local args = {} for v in str:gmatch( "(%S+)" ) do table.insert( args, v ) end --[[ args: is "random shite" and "another arg" ]] [/lua] And this is what I want the args to be: [code] is "random shite" and "another arg" [/code] Could someone help me?
Options: If you're willing to use [url=http://www.facepunch.com/showthread.php?t=750140]LPeg[/url]: [QUOTE=Deco Da Man;15413116]Good example: [lua] -- Convert an argument string to a table bot.patterns.argument = '"' * lpeg.Cs(((lpeg.P(1) - '"') + lpeg.P'""' / '"')^0) * '"' + lpeg.C((1 - lpeg.S'" ')^0) bot.patterns.argument_list = lpeg.Ct(argument*(lpeg.space^1*argument)^0) function bot:ParseArguments(str) return lpeg.match(self.patterns.argument_list, str) -- So much friggen shorter than my original. I love LPeg! end [/lua][/QUOTE] Or there's [url=http://codepad.org/zdzyOtNX]this[/url], by Lexi: [lua]local text = '"this" is some "text which"has quoted "sections in" it' local quote = text:sub(1,1) ~= '"' local ret = {} for chunk in string.gmatch(text, '[^"]+') do quote = not quote if quote then table.insert(ret,chunk) else for chunk in string.gmatch(chunk, "%a+") do table.insert(ret,chunk) end end end for k,v in ipairs(ret) do print(k,v) end [/lua] Enjoy!
Thanks, have a heart :buddy:
[QUOTE=Deco Da Man;17756998]Options: If you're willing to use [url=http://www.facepunch.com/showthread.php?t=750140]LPeg[/url]: Or there's [url=http://codepad.org/zdzyOtNX]this[/url], by Lexi: [lua]local text = '"this" is some "text which"has quoted "sections in" it' local quote = text:sub(1,1) ~= '"' local ret = {} for chunk in string.gmatch(text, '[^"]+') do quote = not quote if quote then table.insert(ret,chunk) else for chunk in string.gmatch(chunk, "%a+") do table.insert(ret,chunk) end end end for k,v in ipairs(ret) do print(k,v) end [/lua] Enjoy![/QUOTE] Hey I was going to post that! :argh:
Sorry, you need to Log In to post a reply to this thread.