Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would you go about stopping string patterns?

Asked by 9 years ago

I am putting some strings inside a table for use with string.find, string.gsub, and possibly some others.

What I want to do is stop the strings from being patterns (the strings I am working with are emotes).

How would I go about this? Is here a certain prefix that you put in front of the string?

Thank you!

~TDP

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You can use % to "escape" any of the special characters and mean the literal character you write. For example, the search pattern "cat%-dog" matches the string "cat-dog" instead of "catttdog" (on a related note, in order to match a literal % you need to use the pattern "%%")

In general, I think you can automatically process patterns like [EDITED to fix replacement]

(pattern:gsub("([^%a%d])", "%%%1"))

to escape all characters in them, though there may be an edge case I'm not remembering. That parenthesis in this case may be significant.

The letters and numbers become special when you put a %, while the special punctuation becomes unspecial, and all of the others remain the same.

string.find

From the function dump, string.find has a fourth parameter, plain, which is whether or not to use patterns (as opposed to just using the plain string).

Thus text:find("cat-dog", 1, true) will find the literal string "cat-dog" instead of "catttdog".

gsub / match / gmatch

As far as I know, there is not a similar plain parameter for these two. You will unfortunately have to manually escape the patterns or manually implement the searches.

Luckily :match is essentially identical to :find when you don't use patterns, since the returned string will just be what you're searching for. gmatch is also very similar, since the only extra information it gives is how many matches have been found.

Emoticons Example

If you have something like :(, the parenthesis is special, so you want to put a % before it: :%(. The colon isn't special, but Lua will just ignore % before punctuation that aren't special -- this makes it easier on you since you don't have to remember. So a pattern for :( could either be %:%( or :%(.

For :), the right parenthesis is sometimes special (only if there was a ( before it) so ":)" should work as a string pattern, but to be safe you can write it at "%:%)" or ":%)".

Using the global-substitution I included earlier is a magic box that can do this automatically for you:

function escape(pattern)
    return (pattern:gsub("([^%a%d])", "%%%1"))
    -- (these parens aren't a mistake and are intentional)
end
smiley = escape(":)")
frowny = escape(":(")
-- etc
0
Can you please tell me how I would do it in context with mine? I'm not sure exactly where to put the %s inside the emote strings. TheDeadlyPanther 2460 — 9y
0
Thank you so much! TheDeadlyPanther 2460 — 9y
Ad

Answer this question