Unknown escape sequence - regular expression

I want to check the email text for which I am using RegexKitLite.h . I do the following:

 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'm just checking if this works. But in the second line I get an error. The magazine says nothing. But in the second line there is a warning saying:

 Unknown escape sequence '\.' 

What could be the problem? Is there something wrong in the expression or is there some other problem?

Thanks,
Nitish

+4
source share
1 answer

You do not need to avoid dots in the character class. Most regex engines ignore this error; Objective-C looks more rigorous. Try the following:

 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 not related to the problem), this is a rather strange regular expression for checking email, as it will reject many valid email addresses and resolve many invalid ones. I don’t know what you are striving for here, but as a rule, it’s nice not to be too strict with checking regular expressions and rather to check by actually sending an email to this address and see if it works.

+7
source

All Articles