Jump to content




[lua]deleting Specific String From A Variable [string.gsub?]


  • You cannot reply to this topic
9 replies to this topic

#1 Goof

  • Members
  • 751 posts

Posted 17 October 2013 - 05: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

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 17 October 2013 - 06: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>","")


#3 Goof

  • Members
  • 751 posts

Posted 17 October 2013 - 06: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>


#4 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 17 October 2013 - 06: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: "%bxy", 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/m...nual.html#5.4.1

#5 Goof

  • Members
  • 751 posts

Posted 17 October 2013 - 06:53 PM

Okay, ill try after i've got some sleep. Thanks for the response :-D

#6 Goof

  • Members
  • 751 posts

Posted 18 October 2013 - 03: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

#7 Goof

  • Members
  • 751 posts

Posted 18 October 2013 - 03: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


#8 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 18 October 2013 - 04:02 PM

View PostMikk809h, on 18 October 2013 - 03:21 AM, said:

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.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 October 2013 - 04:07 PM

View Postkreezxil, on 18 October 2013 - 04:02 PM, said:

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"

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 October 2013 - 11:48 PM

View PostMikk809h, on 18 October 2013 - 03:21 AM, said:

-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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users