Is it possible to escape magic characters and format a regex pattern at the same time?

I have the following string pattern:

(“Attempt to update a read%-only table %s.”):format(tableName)

This pattern is going to be used in a regex search using string.find. “-” is a magical character in regex so I escaped it with “%” . My problem is the format function reads “%-” as a formatting directive despite not being one. I’m aware I can break the string in two and solve this particular problem, but as a general question: Is it possible to escape magic characters and format a regex pattern at the same time?

As string.format is first formatted then the string pattern, you can escape them with %% to format them as a literal %, e.g. %%- would be formatted as %- then that’ll be the string pattern to match.

2 Likes

@blockzez answer is good for what your looking for If I understand correctly what your trying to achieve.

I just want to add on and clarify that you are referring roblox/Lua, this is not regex at all, but rather lua’s own string pattern framework. This should not be referred to as “RegEx” as this causes confusion.

Lua does not follow the regex standards.

Hope this clarifies things for you a little!

I wasn’t aware of that. Thanks for the clarification!

This makes a lot of sense. Thanks for the help!

1 Like