views:

303

answers:

2

Hello Every one.

I want to use regex for the following expression.

here mail id can be any thing. but I just want to cut this string.

I was trying following code.

NSString *email=@"<a href=\"mailto:[email protected]\">";
    NSString *strEmailMatchstring= @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";
    if(![email isMatchedByRegex:strEmailMatchstring]){
        av=[[[UIAlertView alloc] initWithTitle:@"Message" message:@"Invalid Email-ID." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [av show]; return NO;
    }

I know it is wrong. I have placed the regex of checking mail id. I just want to But I don't know about regex.

What should be the correct way?

Thanks in advance for sharing your knowledge.

A: 

Here is an RFC 2822 Compatible Regex

[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 use this one

\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
streetparade
A: 

Yeh. I did it.

NSString *strRegex=@"<a href=\"mailto:\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b\">";
            NSString *tmmpStr=[[[tmpStr stringByReplacingOccurrencesOfString:@" valign=\"top\"" withString:@""] stringByReplacingOccurrencesOfString:@"<b>" withString:@""] stringByReplacingOccurrencesOfString:@"</b>" withString:@""];
            NSString *tmp2=[tmmpStr stringByReplacingOccurrencesOfRegex:strRegex withString:@""];
            NSString *tmp3=[tmp2 stringByReplacingOccurrencesOfString:@"</a>" withString:@""];
            NSLog(@"%@",tmp3);
shivang
If it worked for you accept the answer
streetparade
Can you accept your own answer? I don't think he used your response...
Kendall Helmstetter Gelner
@Kendall Yes, you can - after 48 hours. http://blog.stackoverflow.com/2009/01/accept-your-own-answers/
Amarghosh