Validate Email Account using Regular Expression in Objective-C

http://stackoverflow.com/questions/3179859/regex-for-an-email-address-doesnt-work

头文件

#import <Foundation/Foundation.h>

@interface NSString (Utils)
+ (BOOL)validateEmail:(NSString *)emailAddress;
@end

实现文件

#import "NSString+Utils.h"
@implementation NSString (Utils)
+ (BOOL)validateEmail:(NSString *)emailAddress
{
    if(emailAddress==nil) return FALSE;
    if(emailAddress.length==0) return FALSE;
    
    NSString *stricterFilterString =
    @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
    //@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilterString ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailAddress];
}
@end

 

//////////////////////////////////////////////////////////////////////////////////////

电子邮箱、网址、固定电话号码、银行帐号等常用正则表达式的写法

///////////////////////////////////////////////////////////////////////////////////

email

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

or

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

///////////////////////////////////////////////////////////////////////////////////

 

网址

(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]

 

《一个email正则表达式引发的思考》http://blog.sina.com.cn/s/blog_4c925dca01009m47.html
/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;

///////////////////////////////////////////////////////////////////////////////////


http://zhidao.baidu.com/question/21663454.html
匹配首尾空白字符的正则表达式:^\s*|\s*$  
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式  

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*  
评注:表单验证时很实用  

匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*  
评注:网上流传的版本功能很有限,上面这个基本可以满足需求

///////////////////////////////////////////////////////////////////////////////////

常用的正则表达式
http://www.suzhou35.com/blog/article.asp?id=163
1、非负整数:^\d+$

2、正整数:^[0-9]*[1-9][0-9]*$

3、非正整数:^((-\d+)|(0+))$

4、负整数:^-[0-9]*[1-9][0-9]*$

5、整数:^-?\d+$

6、非负浮点数:^\d+(\.\d+)?$

7、正浮点数:^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$

8、非正浮点数:^((-\d+\.\d+)?)|(0+(\.0+)?))$

9、负浮点数:^(-((正浮点数正则式)))$

10、英文字符串:^[A-Za-z]+$

11、英文大写串:^[A-Z]+$

12、英文小写串:^[a-z]+$

13、英文字符数字串:^[A-Za-z0-9]+$

14、英数字加下划线串:^\w+$

15、E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$

16、URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$
或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$

17、邮政编码:^[1-9]\d{5}$

18、中文:^[\u0391-\uFFE5]+$

19、电话号码:^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$

20、手机号码:^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$

21、双字节字符(包括汉字在内):^\x00-\xff

22、匹配首尾空格:(^\s*)|(\s*$)(像vbscript那样的trim函数)

23、匹配HTML标记:<(.*)>.*<\/\1>|<(.*) \/>

24、匹配空行:\n[\s| ]*\r

25、提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?

26、提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

27、提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?

28、提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)

29、提取信息中的中国手机号码:(86)*0*13\d{9}

30、提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}

31、提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}

32、提取信息中的中国邮政编码:[1-9]{1}(\d+){5}

33、提取信息中的浮点数(即小数):(-?\d*)\.?\d+

34、提取信息中的任何数字 :(-?\d*)(\.\d+)?   

35、IP:(\d+)\.(\d+)\.(\d+)\.(\d+)

36、电话区号:/^0\d{2,3}$/

37、腾讯QQ号:^[1-9]*[1-9][0-9]*$

38、帐号(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$

39、中文、英文、数字及下划线:^[\u4e00-\u9fa5_a-zA-Z0-9]+$

 

///////////////////////////////////////////////////////////////////////////////////

匹配URL网址的正则表达式[a-zA-z]+://[^s]
http://www.wearelearn.net/javascript/jsRegExp_12_52/415.html
比较全面的javascript用正则表达式匹配url网页地址
 var strRegex = "^((https|http|ftp|rtsp|mms)?://)"

  + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@

  + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184

  + "|" // 允许IP和DOMAIN(域名)

  + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.

  + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名

  + "[a-z]{2,6})" // first level domain- .com or .museum

  + "(:[0-9]{1,4})?" // 端口- :80

  + "((/?)|" // a slash isn't required if there is no file name

  + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";

  var re=new RegExp(strRegex);
http://www.wearelearn.net/javascript/jsRegExp_12_52/519.html

posted @ 2012-07-26 14:34  愚茶道长  阅读(552)  评论(0编辑  收藏  举报