Lua Pattern Matching and Magic Characters

At some point in your project when you’re writing Lua code you will probably use various kinds of string manipulation. Some of the most useful manipulations use pattern matching.

My personal favorite is gsub, which takes a string, a pattern you’re looking for in the string, and a replacement string that you wish to use to replace any instances of the detected pattern in the original string. 


   s = string.gsub("Lua is cute", "cute", "great")
    print(s)         --> Lua is great


It’s incredibly useful and allows you to implement interesting dynamic behavior in your code.

However, sometimes people run into issues where everything appears to be set up correctly, but the pattern matching just isn’t working. Anecdotally, the vast majority of these problems are caused by what are called Magic Characters.

For example, let’s say you you want to isolate the file name from the given file path:


So in this case we could perform a gsub operation to isolate just “icon.png”.

This would look like:

local image_name = string.gsub("/Users/Name/Desktop/workspaces/workspace-2022-06/app/images/icon.png", "/Users/Name/Desktop/workspaces/workspace-2022-06/app/images/", "")


This should replace the file path leading up to “icon.png” with an empty string so the returned string is simply just “icon.png”.

However the pattern matching would not work as intended because this string contains the magic character “-”.

This is because magic characters serve a function when interpreted by Lua’s pattern matching.

The full list of this magic characters are:

   ( ) . % + - * ? [ ^ $

If you would like to learn about what all of these characters do, I recommend taking a look at the Lua documentation here:

This doesn’t mean that there is no hope for the pattern matching to work. You can actually use the character “%” to exit any magic character, even itself.

So yes you could modify the workspace name to not include any “-” characters or you could simply modify gsub command like so:

local image_name = string.gsub("/Users/Name/Desktop/workspaces/workspace-2022-06/app/images/icon.png", "/Users/Name/Desktop/workspaces/workspace%-2022%-06/app/images/", "")




Please sign in to leave a comment.

Didn't find what you were looking for?

New post