-1

regular expression need to accept +,-,& need to accept before @ in email validation

([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}) in this regular expression its not accepting.

can any one provide me proper regular expression.

Example:

demo+wifi-mail&name@gmail.com

stema
  • 90,351
  • 20
  • 107
  • 135
Kiran
  • 345
  • 1
  • 4
  • 16
  • 1
    See this : http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address?rq=1 – Lithu T.V May 28 '13 at 05:32
  • http://stackoverflow.com/questions/9276246/how-to-write-regular-expressions-in-objective-c-nsregularexpression – Nitin Gohel May 28 '13 at 05:33

3 Answers3

2

use this one it'l helps you.

([\\w-\\.\\+\\-\\&}]+)@((?:[\\w]+\\.)+)([a-zA-Z]{2,4})

NSString *phone=@"demo+wifi-mail&name@gmail.com";
NSString *pNRegex = @"([\\w-\\.\\+\\-\\&}]+)@((?:[\\w]+\\.)+)([a-zA-Z]{2,4})";
NSPredicate *PNTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pNRegex];
BOOL check=[PNTest evaluateWithObject:phone ];
NSLog(@"%i",check);----> 1
Balu
  • 8,470
  • 2
  • 24
  • 41
  • regular expression changed to ([\w-\.\+\-\&}]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}) Now it will worked for me! – Kiran May 29 '13 at 09:15
1

This [\w\.] is a character class (I removed the - for now). Every character that is within the square brackets is matched by this class. So, your class is matching all letters, digits and underscores (that is done by the \w part) and dots.

If you want additional characters, just add them to the character class, e.g. [\w.+&-].

Be careful with the - character, it has a special meaning in a character class, either escape it or put it at the start or the end.

But be aware, your regex is still not matching all valid email addresses, see the links in the comments.

Single characters (without special meaning) or predefined classes written in a class doesn't make sense, [\w] is exactly the same than \w.

stema
  • 90,351
  • 20
  • 107
  • 135
0

([\w-.+-\&}]+)@((?:[\w]+.)+)([a-zA-Z]{2,4}) Now it will work!

Kiran
  • 345
  • 1
  • 4
  • 16