1

I am pretty much weak in creating Regular Expression. So i am here.

I need a regular expression satisfying the following.

NSString *nameRegex =@"My RegEx"; 
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex]; 
validationResult=[nameTest evaluateWithObject:password];

These are the conditions

  1. It should contain letters and numbers

  2. Minimum 7 characters Max 32 characters

  3. No Space

  4. No Symbols

Please Help..

ipraba
  • 16,485
  • 4
  • 59
  • 58

2 Answers2

6

Here's how you do it !

/^[a-zA-Z0-9]{7,32}$/

Don't forget the "beginning to end" markers.

For the record, if you want it the string to definitely include both letters and digits, there is no way to do that with one regex. (In fact, mathematically you could do it, but the regex would be enormous.)

Simply run the above check, and THEN ALSO CHECK that it contains a letter: /[a-zA-Z]/

And THEN ALSO TEST that it contains a digit: /[0-9]/

That's the easiest way.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • This does not require both letters and numbers - 'password' would be accepted. – ceejayoz Mar 15 '11 at 21:12
  • Hey Joe Blow, note that in 2011 when you posted it, none of that text was there. http://stackoverflow.com/revisions/5318125/1 – ceejayoz May 22 '14 at 20:45
0

As a non-regex expert, I would solve this by splitting up the criteria into separate tests which can then be easier understood and changed, ie multiple if conditions. I believe this cannot be done with a single regex, at least not in an understandable fashion :)

ccjensen
  • 4,578
  • 2
  • 23
  • 25
  • agreed, joe. I am changing my suggested solution to match my new found knowledge. – ccjensen Mar 15 '11 at 21:39
  • yup, I agree Joe. Amazing how easy these things are forgotten. It's the same as why you can only find fixed length palindromes with regex: http://stackoverflow.com/questions/233243/how-to-check-that-a-string-is-a-palindrome-using-regular-expressions – ccjensen Mar 16 '11 at 01:31