Easy way to check if a string consists only of letters?

I’m working on a party system and I’m getting around to filtering what they can use for party names. I found something that checks if the string is alphanumeric but I didn’t see one for checking if it’s only consisting of letters.

This should work:



string.match(str, "^%a+$") ~= nil


This searches for the pattern “starts, contains only one or more letters, and ends”, and string.match returns nil if the pattern isn’t matched.

What exactly are the operators doing? I don’t know if that’s what you explained in quotes or not.

It is, you can read more about them in the Lua manual here.

So the “$” represents the end of the string, and the “^” represents the beginning?

And + means “one or more of the previous character class.” So it expects that the only thing appearing between the start and end are letters. If you want it to be “zero or more”, change it to a *.

[editline]19th January 2017[/editline]

Lua patterns are a strange version of regex, which is a very useful tool in general for programmers to know.

It doesn’t represent them as it anchors the pattern between them, i.e.



print(string.match("1abc123", "^%a+&"))
print(string.match("1abc123", "%a+"))
print(string.match("1abc123", "^%a+"))


will print

Reason being is that, in the first pattern, %a+ has to match the entire string. In the second pattern, %a+ can match anywhere in the string. The third pattern has to match %a+ starting from the beginning of the string.

Thanks guys, I had no idea what these meant until now.