Why gsub isnt working?

Hi,
I have problem, that i need to remove something from string, so i tried this

str, i = string.gsub(str,f,"")

but it isnt working and i is 0, so i tried to add f to string, so i know that f is in the string

str = str..f
str, i = string.gsub(str,f,"")

but still i is 0.
Can you help me?

What is str and f? You seem to be combining str with f but you haven’t defined either of them. Also, gsub only returns 1 item so why do you have i as a second variable?

str, is string and f the substring i need to remove. I didnt post it, but they are defined upper and are not nil (checked in studio)

It worked for me? Are you sure that you have properly defined str, f and i?

local str, f, i = "hello world", "l", 0

str, i = string.gsub(str, f, "")
print(str, i)

In this case I removed all substrings of value j from string str and set i to the amount that were removed.

image

2 Likes

Can you provide us with more code so we can help you?

1 Like

No. gsub returns 2 variables.

1 Like

(string and number of matches)

2 Likes

Can you provide the declarations for str, f, and i? I set up the same code and it worked for me, so perhaps your declarations for each variable are what is causing the problem.

1 Like

Oh, apologies I was looking at the documentation.


Edit: Yikes, I did not read the last part, you are right.

2 Likes

Ik, that it is confusing, bec in function name, there is just 1 var

When i used studio var incpector, it showed this
str=function () {
something
}
digitalWrite(50,true)
function () {
something
}function () {
something
}
(the last one function () {something} was added by the line that is adding f)
f=f function () {
something
}
i=nil
and after it
everything is same, but
i=0

I have no idea what that is, and I am somewhat confused on how to read it. I was not asking for whatever that is, I am asking for how you defined the variables str, j, and i.

str is parametr,
local f = string.sub(str,s,ch2),
local i

And i tried to change f to “i”, and it worked, so here must be the problem.
Edit #1: And i tried string.gsub(f,f,“”), but also didnt work. So the problem can be, that the string includes ().
Edit #2: I tried to remove () and it works. So how to make gsub ignoring it?

Yes it is?

print(string.match(")",")")) --> invalid pattern capture

To sanitize the input, something like this should work

local function sanitize(s)
    return (string.gsub(s,"[%^%$%(%)%%%.%[%]%*%+%-%?]","%%%0"))
end
3 Likes

https://www.lua.org/pil/20.2.html

Above shows you that it’s a magic character with code: this shows you that it’s a magic character with documentation. Just thought to tack that on.

Also here: String Patterns

2 Likes

+ and - seem to have special treatment. i once used + without a % and it actually matched a +.

Did you use them in a position where they couldn’t be a quantifier?

print(string.match("a","+")) --> nil
print(string.match("a","a+")) --> a

A similar thing happens with the anchors ^ and $, when ^ isn’t the first character, it matches a ^, and when $ isn’t the last character, it matches a $. When one of the quantifiers (*,+,-,?) is used outside of a position where it would be interpreted as a quantifier, it’s treated like a non magic character, so it matches it.

1 Like

yeah that was probably it. i didn’t know it worked like that since other magic characters don’t work like that either. but i see why now thank you

1 Like