开发者

Unknown escape sequence - Regular expression

I want to validate email text for which I am using RegexKitLite.h. I am doing things as follows :

NSString *strEmail = [txtEmail text];
                NSRange range = [strEmail rangeOfRegex:@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"];
                NSLog(@"%@",range);  

For now I am just checking if this works. But I am getting error in second line. Log doesn't say anything. But there is a warning in second line which says :

Unknown escape sequence '\.'  

What might be the problem? Is there s开发者_StackOverflow社区omething wrong in expression or is there some other issue?


You don't need to escape the dot in a character class. Most regex engines ignore this kind of error, Objective C's seems to be more strict. Try this:

rangeOfRegex:@"^[a-zA-Z][\w.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z.]*[a-zA-Z]$"

However (and unrelated to the problem), that's a fairly strange e-mail validation regex as it will reject many valid e-mail addresses and allow many invalid ones. I don't know what you're aiming for here, but it is generally a good idea not to be too strict with validating regexes and rather do the validation by actually sending an e-mail to that address and see if it fails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜