767 posts
Posted 17 October 2013 - 06:55 PM
Hello
I dont remember how to "take a specific string out of a variable"
local bigstring = "This is a html script <html> <header> </header> </html>"
local howto = function()
return string.gsub(bitstring,"<html> <header> </header> </html>")
end
shortstring = howto()
print(shortstring) -- I want this to be "This is a html script "
but instead im getting an error :
Bad argument: string/function/table expected
–
I dont remember how to anymore…
Could anyone please tell me ?
Thanks in Advance
1688 posts
Location
'MURICA
Posted 17 October 2013 - 07:04 PM
string.gsub needs a string to replace the matching content
with. Just tell it to replace that with an empty string.
return string.gsub(bitstring,"<html> <header> </header> </html>","")
767 posts
Posted 17 October 2013 - 07:18 PM
well… Okay… That works in the example i made
But… If i received a message from a website which is:
<header>
<title>
Mikk809h - Confirmation Access Terminal
</title>
</header>
<h2>
<font color="red" face="Comic sans MS">
This file server is password-protected! Any access is logged
</font>
</h2>
I've posted all that code into the string to search for, but that still returns the whole string for the website.. (the code i showed you)
Hmm? Wierd?
EDIT:
String in the search for my gsub:
Invalid access.(keyfire)<html> <header> <title> Mikk809h - Confirmation Access Terminal </title> </header> <body> <h2> <font color="red" face="Comic sans MS"> This file server is password-protected! Any access is logged </font> </h2> </body></html>
1688 posts
Location
'MURICA
Posted 17 October 2013 - 07:48 PM
When you use the string that you have there, it specifically searches for the text "<html> <header> </header> </html>", which is unlikely to show up in any web page.
It looks like you're just looking to remove HTML tags, or any characters between two arrow brackets. Lua's pattern matching has a nice convention for this: "%b
xy", which matches text between two "balanced" characters,
x and
y. Example:
str = "<some html code>hello!</some html code>"
print(str:gsub("%b<>", "")) --> hello!
The code above matches any text between < and > characters, including these characters.
More on patterns:
http://www.lua.org/manual/5.1/manual.html#5.4.1
767 posts
Posted 17 October 2013 - 07:53 PM
Okay, ill try after i've got some sleep. Thanks for the response :-D
767 posts
Posted 18 October 2013 - 04:21 AM
Hmm.. this test I just made returns :
test:2: vm error:
java.lang.ArrayIndexOutOfBoundsException: 65
?
code:
str = "<html>This is funny; Hi... This is also funny; No its not </html>";
print(str:gsub("%b<>",""))
ehh
767 posts
Posted 18 October 2013 - 04:26 PM
Well; for now I've just used the old "string.find" method…
code:
if content:find("Invalid access")
then
term.setTextColor(colors.red)
print("ACCESS DENIED!")
elseif content:find("Access granted to main file server")
then
term.setTextColor(colors.lime)
print("ACCESS GRANTED")
else
error("Oooops! Something terrible happened!",2)
end
187 posts
Location
Bowie, TX
Posted 18 October 2013 - 05:02 PM
Hmm.. this test I just made returns :
test:2: vm error:
java.lang.ArrayIndexOutOfBoundsException: 65
?
code:
str = "<html>This is funny; Hi... This is also funny; No its not </html>"; <--- look here for the possible error
print(str:gsub("%b<>",""))
ehh
Looks like you have an extra '
;' at the end of your test string.
7508 posts
Location
Australia
Posted 18 October 2013 - 05:07 PM
Looks like you have an extra ';' at the end of your test string.
No… just no… If you're going to help in Ask a Pro, at least know what you're talking about! In Lua a line can optionally be ended with a semi-colon.
EDIT: Oh and even if that were the problem, the error would have been a compile error due to invalid syntax, would have said something like "unexpected symbol"
7508 posts
Location
Australia
Posted 19 October 2013 - 12:48 AM
-snip-
You could always just use two gsub's to remove open then close tags
local str = "<html>This is funny; Hi... This is also funny; No its not</html>";
local noopen = str:gsub("^<.->","") -- remove opening tags
local noclose = noopen:gsub("</.->", "") -- remove closing tags
print(noclose)
EDIT: Oops, I meant to click edit, sorry for double post.