• Capturing "
    8 replies, posted
Hi there, I have run into a slight problem with patterns or reading in what players say, as I need to be able to capture the " for example if the player enters spawn(apple) into a textbox it will spawn an apple, but what I want is for the player to have to enter spawn("apple"). So I figure my question is.. how would I make it will capture everything within ("") instead of just () So far I have: [lua] local function_name, phrase = Text:match( "([%w_{]+)%(([%w_]+)%);" );[/lua] which works perfectly, but of course without the " " Hope that makes sense and cheers!
\"?
[QUOTE=Lexic;18305426]\"?[/QUOTE] D'oh I forgot to mention I tried that, all it did was make the player use secondary fire for some reason.. lol
Just define the string using teh doubly bracket method. [[ stuff ]] An added bonus is that you can have multiple lines in one string. [[stuff ZOMG A NEW LINE11111! ]] Hope that helps
[lua]'Look! I am "matching" double quotes!'[/lua] Untested but it might work! :ninja:
Here's a function I wrote for Theo to get arguments from a chat command. Perhaps you can apply it to your situation somehow. [lua] local function ChatVariables(text) local subtext = string.Explode(" ", text); local vars = {}; -- Now that we've got our variables, we need to combine things that have quotes around them while #subtext > 0 do local startVar = 0; local endVar = 0; for k,v in pairs(subtext) do if endVar == 0 then if string.sub(v, 1, 1) == "\"" and startVar == 0 then -- This is the start of a var. startVar = k; end if string.sub(v, 1, 1) != "\"" and startVar == 0 and endVar == 0 then startVar = k; endVar = k; end if string.sub(v, string.len(v), string.len(v)) == "\"" and startVar != 0 then -- This is the end of a var. endVar = k; end if k == #subtext and startVar != 0 then -- It's the end of the entire string, SOMEBODY forgot a quote! endVar = k; end end end if startVar != 0 and endVar != 0 then local diff = endVar - startVar; local var = ""; for i=0,diff do var = var .. string.gsub(subtext[startVar+i], "\"", "") .. " "; end for i=0,diff do table.remove(subtext, endVar-i); end table.insert(vars, string.sub(var, 1, string.len(var) - 1)); end end return vars; end [/lua] So say for example you put in !command "asdf" asdf "asdf asdf", you would get the variables asdf, asdf, and asdf asdf. You have to pass only what comes after "!command " though.
Thanks for all the replies, I have worked out a way to do it now <3
[QUOTE=Nori;18308738]Here's a function I wrote for Theo to get arguments from a chat command. Perhaps you can apply it to your situation somehow. [lua] c0de [/lua] So say for example you put in !command "asdf" asdf "asdf asdf", you would get the variables asdf, asdf, and asdf asdf. You have to pass only what comes after "!command " though.[/QUOTE] [lua] local quote = text:sub(1,1) ~= '"' local tab = {} for chunk in text:gmatch'[^"]+' do quote = not quote if quote then table.insert(tab,chunk) else for chunk in chunk:gmatch"%a+" do table.insert(tab,chunk) end end end[/lua]
I know absolutely nothing about gmatch so thanks a bunch for that. :smile:
Sorry, you need to Log In to post a reply to this thread.