This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
houseofkraft's profile picture

Cannot remove ()'s in a string using gsub

Started by houseofkraft, 10 November 2016 - 06:48 PM
houseofkraft #1
Posted 10 November 2016 - 07:48 PM
Hi Guys!

I have a string and it contains print("Hi"). I am trying to remove everything in that statement except the actual thing it is trying to print.

I tried this

str:gsub('("', "")

but nothing worked sadly. When i do that, it returns (Hi)

Thanks!
H4X0RZ #2
Posted 10 November 2016 - 08:58 PM
string.gsub uses patterns AFAIK and "(" and ")" are special characters. Try this:

str:gsub("%(","")

But you could also do something like this:

local what = str:match "print%((.+)%)"

This matches (or atleast should) everything inside the parantheses.
Edited on 10 November 2016 - 08:00 PM
Sewbacca #3
Posted 11 November 2016 - 10:08 AM
Or try:
str:match 'print(%b())'
but note that calls can also be:
print ''
print ""
print [[]]
print {}
print ()
Edited on 12 November 2016 - 11:20 PM
H4X0RZ #4
Posted 11 November 2016 - 01:50 PM
Or try:
str:match 'print%b()'
but note that calls can also be:
print ''
print ""
print [[]]
print {}
print ()

Your pattern doesn't work. Partially for the same reason OP's pattern won't work.


Sewbacca #5
Posted 13 November 2016 - 12:21 AM
I forgot some brackets, thanks ^^.
I edited it in my preview post.
Edited on 12 November 2016 - 11:22 PM