How to use string:sub()

Hello.
Those “random characters” are string patterns. As explained above, () (parentheses) indicate that we want to save whatever is in them into a variable.
Some of the patterns that I used in my previous post:

  • ^ matches everything at the beginning of string, $ matches everything at the end of string, and ^...$ matches the whole string.
  • %w matches all alphanumeric characters (letters and numbers). I know that usernames can also have underscores, but you can add them yourself with sets []: [%w_]+. This will match both alphanumeric characters and underscores.
  • %s matches all whitespaces.
  • + matches 1 or more of the specified character class (in our case %w).
  • - matches as few of the specified character classes as possible (in our case %s, we don’t need to match any whitespaces between the arguments).

I recommend reading this article to get to know string patterns better, as they are very interesting and useful. You can also find a list of all magic characters and patterns there.

9 Likes