Get all strings in a script by using patterns

What I actually want to do is use the “Source” property or a script and use string.gsub to do some things with it. But nothing I want to do should happen on strings, so I need to first match for everything that’s not a string. If I have a way to get everything that is a string, then I can inverse the result someway.
So I made this Regex regex101: build, test, and debug regex
It works great! But now I need to get this in Lua string patterns… I can’t think of any way to do this because Lua is missing really nice features like OR. Please help

What do you define as “not a string”? Do you mean strings with non-alphanumeric characters? Trying to help see if there is a simpler expression that achieves what you want.

“not a string” is anything that is not the color of the a string in your script editor.
If you view the link and in the “TEST STRING” section you paste one of your scripts you can see that it works to highlight all strings.
I am trying to make a plugin so the pattern needs to be strong.

EDIT: I am open to using functions as the replace parameter in string.gsub

1 Like

For finding every string in a script you can do something like this:

local Pattern = "[%'?%\"?](.-)[%'?%\"?]"
local Source =  "local 5 = '5'"

for String in Source:gmatch(Pattern) do
	print(String)
end

The Pattern [%'?%\"?](.-)[%'?%\"?] optional matches either a quote or a double quote on either side of the string, meaning that it can still match string that looks like: "Hello World', if you don’t want that you can change [%'?%\"?] on both sides to just %' or %\"

1 Like

Thank you, It doesn’t work for all string types, but I can add on.
How would I go about inverting this match?

I got something that is a start, I need to edit it though, the pattern needs to know what ["?‘] actually matches so that the next time it uses ["?’] it can just be [%1], but it does not work like that.
It also has to be aware of strings that escape with a backslash. Then it also needs to work with other string declarations in Lua. I don’t like the loop though.

''
""
[[]]
[==[]==]

ikr. the last two are not escapable with a backslash

local Pattern = "(.*)[\"?'].*[\"?']"
local Source =  "local g = '56'mess'ds'a''ge"
repeat
	local count
	Source, count = string.gsub(Source, Pattern, "%1")
until count == 0

print(Source)
-->> local g = message