How would you go about removing comments from this
local scr = [[ local t = "hello world" -- cool local f = "world hello" -- removing comments ]] print(scr) --outcome (supposed to be) local t = "hello world"local f = "worldhello" instead of //--\\ local t = "hello world" -- cool local f = "world hello" -- removing comments
I tried using string.gsub but also failed, also couldn't find any articles to help me
people are saying this is a solution, but I'm trying to make this
comment_symbols = "--" s1 = [[ "cool" -- lol "man" ]] print ( string.match( s1, "[^"..comment_symbols.."]+" ) ) -- outputs cool
im trying to make this output "cool man" instead of just "cool"
Hello everyone, I know this reply is a year late but this is what I came up with:
local astring = [[ local a = { "foo", -- fa fa fa fa fa "bar" } ]] print(astring:gsub("--(-.-)\n","\n"))
This prints out:
local a = { "foo", "bar" }
I'm a year late, yay.
We need it to ignore string formats because it will mess up the code, right?
This is the multi-lined version
local function GetMutliLinedComments(code) local commentfound = false -- if the comments found then it will turn true local start_ = 0 -- the start of the comment local end_ = 0 -- the end of the comment local sub = 0 -- its use is to get the next letter(or word) local ignore = false -- if the function can ignore the comment inside a format local comments = {} code:gsub(".",function(c) -- usin gsub( well it's kinda bad cuz it has a limit sub = sub + 1 -- what if c == '"' then -- if it is a string format then if ignore then ignore = false else ignore = true return -- not gonna do anything end end if c == "'" then-- if it is a string format then if ignore then ignore = false else ignore = true return end end if code:sub(sub,sub+1) == "[[" and code:sub(sub-2,sub+1) ~= "--[[" then-- if it is a string format then if ignore then ignore = false else print(code:sub(sub-2,sub+1)) ignore = true return end end if not ignore then -- here comes the fun part if code:sub(sub,sub+3) == "--[[" and commentfound == false then -- finds a comment format and no comments are found then commentfound = true -- a comment is found! start_ = sub end if code:sub(sub,sub+1) == "]]" and commentfound == true then -- finds teh end of a comment format and a comment format is found then commentfound = false -- if it's the end of the format no comments are found end_ = sub+1 -- just a little bug table.insert(comments,code:sub(start_,end_)) -- inserts the comment format into a table end end end) return (comments) -- returns the table end
String.match will work just fine here, just implement it correctly.
http://lua-users.org/wiki/PatternsTutorial https://www.rosettacode.org/wiki/Strip_comments_from_a_string