How would you go about removing comments from this
1 | local scr = [[ |
2 | local t = "hello world" -- cool |
3 | local f = "world hello" -- removing comments |
4 | ]] |
5 | print (scr) --outcome (supposed to be) local t = "hello world"local f = "worldhello" |
6 | instead of // --\\ local t = "hello world" -- cool |
7 | 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
1 | comment_symbols = "--" |
2 |
3 | s 1 = [[ |
4 | "cool" -- lol |
5 | "man" |
6 | ]] |
7 |
8 | print ( string.match( s 1 , "[^" ..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:
1 | local astring = [[ |
2 | local a = { |
3 | "foo", -- fa fa fa fa fa |
4 | "bar" |
5 | } |
6 | ]] |
7 |
8 | print (astring:gsub( "--(-.-)\n" , "\n" )) |
This prints out:
1 | local a = { |
2 | "foo" , |
3 | "bar" |
4 | } |
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
01 | local function GetMutliLinedComments(code) |
02 | local commentfound = false -- if the comments found then it will turn true |
03 | local start_ = 0 -- the start of the comment |
04 | local end_ = 0 -- the end of the comment |
05 | local sub = 0 -- its use is to get the next letter(or word) |
06 | local ignore = false -- if the function can ignore the comment inside a format |
07 | local comments = { } |
08 | code:gsub( "." , function (c) -- usin gsub( well it's kinda bad cuz it has a limit |
09 | sub = sub + 1 -- what |
10 | if c = = '"' then -- if it is a string format then |
11 | if ignore then |
12 | ignore = false |
13 | else |
14 | ignore = true |
15 | return -- not gonna do anything |
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