-1

I want regex with following conditions,

  1. there should be at-least one character or number in the string.
  2. combination may contain special characters like !@#$%&*.
  3. string length should be more than 7.

Eg.

  1. test1234
  2. test@1234
  3. 1234Test

Thanks in advance. here my code

NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^(?[0-9])(?[a-zA-Z]){7,}$"];

BOOL isvalid = [regex evaluateWithObject:string];

sorry i couldn't find regex which i used before.

progrenhard
  • 2,333
  • 2
  • 14
  • 14
user976659
  • 61
  • 1
  • 1
  • 6
  • 2
    i tried google, but solutions doesn't work. i tried regex like [A-Za-z0-9]1{7,}$ ^(?[0-9])(?[a-zA-Z]){7,}$ – user976659 Sep 19 '13 at 15:24
  • Have you tried any code on your own? Normally the site is more helpful once you have shown some effort of solving the problem on your own and hit snags, instead of saying, I googled and nothing works. Try some things, then provide some examples of what you try and what is wrong. Maybe then you will stumble into the answer in trying things, instead of just asking for code. – Walls Sep 19 '13 at 15:26
  • I tried on my own didn't manage to create proper regex, google & stack overflow is providing good regex, but majority of them are for javascript & for .net. – user976659 Sep 19 '13 at 15:35
  • Most of the ones should work cross platform with only minor differences on some of the tricky parts like look-aheads/behinds. For a basic regex like this, it is probably more of an issue of escaping chars correctly then the syntax. – Walls Sep 19 '13 at 15:36
  • 1
    **Show us what you've tried so far.** Don't describe it, but edit the question and paste in the actual code. Then tell us what didn't work. What happened when you tried it? Did you get incorrect results? Did you get *no* results? If the results were incorrect, what made them incorrect? What were you expecting instead? Did you get *any* correct results? If so, what were they? Don't make us guess. – Andy Lester Sep 19 '13 at 15:55
  • Oh no, another bad password validator in regex? Who comes up with these requirements? – tripleee Sep 19 '13 at 17:11
  • possible duplicate of [Password validation regex](http://stackoverflow.com/questions/5068843/password-validation-regex) – tripleee Sep 19 '13 at 17:12

2 Answers2

8

You are close with the lookaheads you have in the second example in your comment. The one you have is looking for a digit [0-9], but it is saying it must start with that digit (not checking for your letters either).

Try this: ^(?=.*[a-zA-Z\d].*)[a-zA-Z\d!@#$%&*]{7,}$

Using your examples listed I did some testing with it. The regex is a combination of the two you found.

  1. Lookahead and see if there is at least one letter or number (?=.*[a-zA-Z\d].*). By having the .* you allow the check to be passed at any point in the string, not just the beginning. You then are saying the regex needs to meet the check before it continues. Think of it as an if clause, if the string contains a letter or number at any point, keep going.

  2. Once you ensure there is a letter or number (at least one in the entire string), match on letters, numbers, and the set of special chars. at least 7. [a-zA-Z\d\!@#\$%&\*]{7,}. This part is the easy part as you just setup the rules and say, give me at least 7 of these.

Walls
  • 3,972
  • 6
  • 37
  • 52
  • well your regex works fine. . I tried to develop my own regex `code`^(?:[0-9]+[a-zA-Z]|[a-zA-Z]+[0-9])[a-z0-9#$@!]*$`code` – user976659 Sep 19 '13 at 17:24
  • That is why I tried to break down the pieces for you, that way if your requirements changed you understand the code and how to tweak it for your needs. Glad you got it working. – Walls Sep 19 '13 at 17:56
3

For the at-least-one thing, you can use lookahead. You seem to have figured out the rest (character class, repetition, anchors) already.

/^(?=.*[a-z0-9])[a-z0-9!@#$%&*.]{7,}$/i
Bergi
  • 630,263
  • 148
  • 957
  • 1,375