String Pattern Not Working Correctly

I am trying to use string patterns to detect whether a character is punctuation (-, !, ; etc), a alphabetical letter (a,b,c etc), or a whitespace (space bar).

The problem is that the script says that “a” is punctuation not a letter.

for w in string.gmatch(lowerInput, "[%a%s%p]") do -- W = Letter + Number | S = Whitespace
		if w == " " then
			assembleEncryptedString(" ")
		else
			if string.gmatch(w,"%p") then
				print("Punctuation - "..w)
				assembleEncryptedString(w)
			else
				print("Letter - "..w)
				local index = table.find(alphabet,w)
				local reversedIndex = 27-index
				local reversedLetter = alphabet[reversedIndex]
				assembleEncryptedString(reversedLetter)
			end
		end
	end

These functions might help:

local function getLettersOnly(String)
	local withoutNumbers = string.gsub(String, "[%d+]", "")
	return string.gsub(withoutNumbers, "[%p+]", "") -- removes puncuation.
end

local function getNumbersOnly(String)
	local withoutLetters = string.gsub(String, "[%a+]", "")
	return string.gsub(withoutLetters, "[%p+]", "") -- removes puncuation.
end

local function getPunctuationOnly(String)
	local withoutLetters = string.gsub(String, "[%a+]", "")
	return string.gsub(withoutLetters, "[%d+]", "") -- removes numbers.
end

In your conditional that checks if the input is punctuation, you have accidentally written string.gmatch instead of string.match, which subsequently returns a truthy value regardless of whether or not it gets any hits, so the input is being mistakenly considered as punctuation.

I tried this, however I got this error:

Line 20:

for w in string.match(lowerInput, "[%w%s]") do -- W = Letter + Number | S = Whitespace

The string.gmatch in line 20 is correct, I meant the string.gmatch in this line:

if string.gmatch(w,"%p") then

That one should just be string.match.

Omg. Thank you so much. I have been trying to get this to work for so long.Ty ty ty

Can you please tell me what I did wrong with this one though?

elseif string.match(w,"%d") then
				print("Digit!")

when I do a number it doesnt do anything. Letters/punctuation still work.