• Striping out words / phrases from a string
    6 replies, posted
i have made two scripts with what i want to do, is having a apache2 like system in garrys mod for roleplay and so far i am able to remove some words out the code, to runstring it, which works, but i want to strip everything from <bla> to </bla> out, including in between them too, [CODE]local mystring = [[<?lua print('\ntest') concommand.Add("test", function() print("\nconcommand works") end) RunConsoleCommand("test")lua?>]] local start1 = string.Split( mystring, "<?lua" ) local end1 = string.Split( mystring, "lua?>" ) local start2 = string.Split( mystring, "<?lua" ) local end2 = string.Split( table.concat( start2, " " ), "lua?>" ) PrintTable( start1 ) PrintTable( end1 ) PrintTable( end2 ) print( table.concat( end2, " " )) local cmd = table.concat( end2, " " ) RunString(cmd) [/CODE] that is the script that works, what im doing instead of remaking php in lua, im using just lua instead, which works as intended, but [CODE]local mystring2 = [[<html> Testing HTML Compat</html><?lua print('\ntest') concommand.Add("test", function() print("\nconcommand works") end) RunConsoleCommand("test")lua?>]] local start1 = string.Split( mystring2, "<?lua" ) local end1 = string.Split( mystring2, "lua?>" ) local start2 = string.Split( mystring2, "<?lua" ) local end2 = string.Split( table.concat( start2, " " ), "lua?>" ) PrintTable( start1 ) PrintTable( end1 ) PrintTable( end2 ) print( table.concat( end2, " " )) local cmd = table.concat( end2, " " ) RunString(cmd) [/CODE] doesn't because of <html> and </html> is not valid lua, what i want to do is remove <html> and </html> and the contents in between so just runstring the code in between <?lua and lua?>, if anyone can help, please post what you do to make it work, and how? so i can learn more about glua or just lua in general [editline]18th July 2017[/editline] just saying most of the code is, just for debugging
You should read up about [URL="http://wiki.garrysmod.com/page/Patterns"]patterns[/URL].
just found this [CODE]local s = '<!-- some other words -->' s = s:gsub('<!%-%-(.-)%-%->',' '))[/CODE] to replace everything in between <!-- -->, but that means i need to remove <!-- --> it self [editline]18th July 2017[/editline] but i dont understand '<!%-%-(.-)%-%->', how can i change from <!-- --> to be <html> </html> or <body> </body> in case there is no html tags
[QUOTE=DarkAussie;52481374]just found this [CODE]local s = '<!-- some other words -->' s = s:gsub('<!%-%-(.-)%-%->',' '))[/CODE] to replace everything in between <!-- -->, but that means i need to remove <!-- --> it self [editline]18th July 2017[/editline] but i dont understand '<!%-%-(.-)%-%->', how can i change from <!-- --> to be <html> </html> or <body> </body> in case there is no html tags[/QUOTE] As mentioned in Sean Bean's post you should take a look at [URL="http://wiki.garrysmod.com/page/Patterns"]patterns [/URL]. I can explain how the one you listed works [CODE] <! //Looks for "<!" in the string %-%- //Looks for the two "-" in the string (.-) //Looks for any characters in between %-%- //Looks for the two ending "-" in the string > //Looks for the ">" on the end [/CODE]
so would this work? [CODE]local s = '<html> <head> some other words </head> <body><p><div></div></p></body></html>' s = s:gsub('<html>(.-)<html>',' ')[/CODE]
[QUOTE=DarkAussie;52483501]so would this work? [CODE]local s = '<html> <head> some other words </head> <body><p><div></div></p></body></html>' s = s:gsub('<html>(.-)<html>',' '))[/CODE][/QUOTE] Did you test it?
yes just tested it, but it doesn't work [editline]19th July 2017[/editline] s:gsub i dont think works in gmod [editline]19th July 2017[/editline] wait i just fixed it, so it actually removes from <!-- to --> completly and <!-- --> it self [editline]19th July 2017[/editline] [CODE] local s = '<!-- some other words --> <?lua lua?>' s = s:gsub('<!%-%-(.-)%-%->',' ') print( s ) local s2 = '<html> some other words </html> <?lua lua?>' s2 = s2:gsub('<html>(.-)</html>',' ') print( s2 ) [/CODE] the output is <?lua lua?>, now, which it what i want [editline]19th July 2017[/editline] it now even removes <html><head></head><body><div></div></body> completely now, thanks for explaining patterns @Kevlon
Sorry, you need to Log In to post a reply to this thread.