Deleting characters matching a pattern

Lua:
Deleting characters matching a pattern

How to:

In Lua, we can use the gsub function to replace occurrences of a pattern with another string – an empty string when we want to delete them:

local text = "Hello, 123 World! 456"
local pattern = "%d" -- pattern that matches all digits
local cleanedText, numOfReplacements = text:gsub(pattern, "")

print(cleanedText) -- Output: "Hello,  World!"
print("Number of replacements made:", numOfReplacements) -- Output: "Number of replacements made: 6"

Notice that gsub also returns the number of replacements made, which can be handy information.

Deep Dive

Lua patterns are simpler than regular expressions found in other languages but are still powerful. Historically, Lua’s decision to implement a lighter pattern-matching mechanism is rooted in keeping the language both lightweight and fast.

Alternatives include using loops with string.find and string.sub to manually inspect and manipulate strings, but this is generally less efficient than pattern matching with gsub.

Implementation-wise, when gsub is called with a pattern, Lua internally compiles this pattern into a bytecode which is then executed by the pattern matcher. It’s worth noting that there’s a distinction between Lua patterns and true regular expressions, with the former having a smaller feature set which excludes constructs like look-aheads or back-references.

See Also

Remember, these tools will help solidify your understanding of Lua’s pattern matching and give you a sandbox to test your string manipulations. Happy coding!