String.match, but retrieve everything else except a certain string?

I am having some difficulty trying to retrieve everything else in a string, except for certain characters in the string. I have been trying to figure out a way using string patterns but there are no character classes for, e.g. Get all characters from the word ‘problem’ except for the letter ‘m’.

Any help would be greatly appreciated. :slightly_smiling_face:

Capture groups, which are a completely unrelated problem to OP

In your match pattern, if you surround the thing you want returned with parentheses (), they will be returned instead of the entire match.

local res = ("problem"):match("(.*)m")
print(res) -- proble

You can have multiple pairs of parentheses inside the match pattern to return multiple things.

local d = "Monday, 25 Feb 2019"
local day, month, year = d:match(".*, (.*) (.*) (.*)")
print(day, month, year) -- 25 Feb 2019

See the Captures section of this tutorial for more info.

EDIT: Aaaa, nevermind. You’re asking for something entirely different. If you want to match anything BUT m in your example, you can define a character class like you normally would for regex by surrounding it in square brackets:

local t = "problem"
local p = "[abcdefghijklnopqrstuvwxyz]*" -- m is omitted from this.
print(t:match(p)) -- proble

You can shorten this a lot by combining two character ranges.

local t = "problem"
local p = "[a-ln-z]*" -- m is skipped.
print(t:match(p)) -- proble

EDIT-2: I think responses to your problem would be better if we knew what you were trying to do in the first place. If all you wanted to do is remove the letter m, then you can use gsub instead of using match/gmatch.

Another solution would be using string.gmatch and [^m]* (all non-m characters) since you might wants numbers and symbols as well. (XAXA’s solution only works with lowercase letters.) You’d have to use gmatch because in something like “meme” there’s multiple non-m characters separated by "m"s.

Thank you XAXA and posatta, both solutions are great for what I am trying to achieve. I appreciate you helping me :grinning: