Jump to content




Cannot remove ()'s in a string using gsub


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

#1 houseofkraft

  • Members
  • 170 posts
  • LocationUSA

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!

#2 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

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 by H4X0RZ, 10 November 2016 - 09:00 PM.


#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

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 by Sewbacca, 13 November 2016 - 12:20 AM.


#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 11 November 2016 - 01:50 PM

View PostSewbacca, on 11 November 2016 - 10:08 AM, said:

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.

Posted Image
Posted Image

#5 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 13 November 2016 - 12:21 AM

I forgot some brackets, thanks ^^.
I edited it in my preview post.

Edited by Sewbacca, 13 November 2016 - 12:22 AM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users