Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Removing comments from a script?

Asked by
Imperialy 149
5 years ago
Edited 5 years ago

How would you go about removing comments from this

1local scr = [[
2local t = "hello world" -- cool
3local f = "world hello" -- removing comments
4]]
5print(scr) --outcome (supposed to be) local t = "hello world"local f = "worldhello"
6instead of //--\\ local t = "hello world" -- cool
7local 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

1comment_symbols = "--"
2 
3s1 = [[
4"cool" -- lol
5"man"  
6]]
7 
8print ( string.match( s1, "[^"..comment_symbols.."]+" ) ) -- outputs cool

im trying to make this output "cool man" instead of just "cool"

0
Are you talking about the comments highlighted in green or variables? DesignerOfGames -7 — 5y
0
green Imperialy 149 — 5y
0
I think ctrl + f can find and replace stuff in a script. So just put in -- or [[/]] and replace it with nothing Joshument 110 — 5y
0
I think .gsub is too weak for this, regex would work perfectly fine but regex itself is bigger than Lua itself. Either you can implement 4000 lines of regex yourself, or ignore them. Fifkee 2017 — 5y

3 answers

Log in to vote
0
Answered by 4 years ago

Hello everyone, I know this reply is a year late but this is what I came up with:

1local astring  = [[
2local a = {
3    "foo", -- fa fa fa fa fa
4    "bar"
5}
6]]
7 
8print(astring:gsub("--(-.-)\n","\n"))

This prints out:

1local a = {
2    "foo",
3    "bar"
4}
0
I don't care if you're a year late MiAiHsIs1226 189 — 4y
Ad
Log in to vote
0
Answered by 3 years ago

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

01local 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
View all 49 lines...
Log in to vote
-2
Answered by 5 years ago

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

0
im trying to remove the whole line of comment and nothing else Imperialy 149 — 5y
1
Have you even checked the second link? Ashley_Phantom 372 — 5y
0
HAVE YOU EVEN CHECKED THE SECOND LINK?! Fifkee 2017 — 5y
0
second link doesnt show anything Imperialy 149 — 5y

Answer this question