Jump to content

[HELP]Characters


AncienT

Recommended Posts

Posted (edited)

You could using string.match to ensure that from start to end, only letters (%a) and the underscore (_) symbol are allowed. E.g. 

return string.match(str, "^[%a_]+$") == str

^ matches beginning of string, [...] makes a set, %a includes all letters (uppercase and lowercase) into the set, _ adds underscore into the set, the + means it can match more than once, and $ matches the ending of the string. The function returns the matched string, which should be identical to the input string since we're matching from start to end. It returns a nil if nothing matched, that is, the input string wasn't from start to end only letters and underscores. It is very lenient and allows names even like Test__, or __john_sMiTh__, etc. No enforcement of where the underscore can be, how much can there be, and no enforcement of capitals.

If you want it to be a little more rigid, for example, that the underscore must separate two different words made of letters, you could use the following

return string.match(str, "^%a+_%a+$") == str

and if you want to enforce capital letters at the beginning of these two words,

return string.match(str, "^%u%l+_%u%l+$") == str -- must begin with capital letter and all subsequent must be lowercase

return string.match(str, "^%u%a+_%u%a+$") == str -- must begin with capital letter and all subsequent can be either case

 

Edited by MrTasty

Previously known as MrTasty.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...