produces an error. More specifically its a malformed pattern error. How do you quote a block comment’s beginning and end separately without producing an error?
…@kyuoibi I think this is what you’re after. The first set of code will print “sleeping code” and the starting and ending comment lines. The second set of code will print only the “sleeping code” line. In order to use what’s referred to as magic characters, they have to be proceeded by the % character. The magic characters are ^ $ ( ) % . [ ] * + - ? so you needed the % in front of each of your find pattern characters.
--[[
sleeping code
--]]
-- this code will print the "sleeping code" line and the starting and ending comment lines.
str = debug.getinfo(1).source
str=string.sub(str,string.find(str,"%-%-%[%["),(string.find(str,"%-%-%]%]")+4))
print(str)
-- this code will print only the "sleeping code" line.
str = debug.getinfo(1).source
str=string.sub(str,(string.find(str,"%-%-%[%[")+4),(string.find(str,"%-%-%]%]")-1))
print(str)
--[[
sleeping code
--]]
--[[
what the
--]]
str = debug.getinfo(1).source
a=1
while a~=nil do
x=string.find(str,"%-%-%[%[",a)
if x==nil then
return
end
y=string.find(str,"%-%-%]%]",a+1)
if y==nil then
return
end
a=y
str1=string.sub(str,x,y+4)
print(str1)
end
--[[
end of code
--]]
… @kyuoibi Actually I have an error in the above program. The line with y+4 should be y+3 . If you change the above program and put both comments back to back with no space between them (see below), you will show the first - of the second comment at the end of the first comment. By changing the 4 to a 3, it works the correct way.
--[[ sleeping code --]]--[[ what the --]]
str = debug.getinfo(1).source
a=1
while a~=nil do
x=string.find(str,"%-%-%[%[",a)
if x==nil then
return
end
y=string.find(str,"%-%-%]%]",a+1)
if y==nil then
return
end
a=y
str1=string.sub(str,x,y+3)
print(str1)
end
--[[
end of code
--]]