AncienT Posted November 27, 2018 Share Posted November 27, 2018 How to allow all the letters and the character you want in lua, for example I want that only letters can be written and the character "_" Link to comment
Dimos7 Posted November 27, 2018 Share Posted November 27, 2018 You want it for gui text or something? Link to comment
Skully Posted November 28, 2018 Share Posted November 28, 2018 Use string.find to check what characters are in a string. Link to comment
Addlibs Posted November 29, 2018 Share Posted November 29, 2018 (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 November 29, 2018 by MrTasty 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now