I’m trying to create a Regex to check for 6-12 characters, one being a digit, the rest being any characters, no spaces. Can Regex do this? I’m trying to do this in objective-c and I’m not familiar with Regex at all. I’ve been reading a couple tutorials, but most are for matching simple cases of a number, or a set of numbers, but not exactly what i’m looking for. I can do it with methods, but I was wondering if it that would be too slow and I figured I could try learning something new.
asdfg1 == ok
asdfg 1 != ok
asdfgh != ok
123456 != ok
asdfasgdasgdasdfasdf != ok
It seems that you mean “letter” when you say “character”, right? And (thanks to burning_LEGION for pointing that out) there may be only one digit?
In that case, use
Explanation:
[^\W_]might look a little odd. How does it work? Well,\wmatches any letter, digit or underscore.\Wmatches anything that\wdoesn’t match. So[^\W](meaning “match any character that is not not alphanumeric/underscore”) is essentially the same as\w, but by adding_to this character class, we can remove the underscore from the list of allowed characters.