2010-01-22 36 views
1

我想用正則表達式來表達下面的表達式。正則表達式這個<a href="-----------------"> iPhone NSString

這裏郵件ID可以是任何東西。但我只是想削減這個字符串。

我正在嘗試下面的代碼。

NSString *[email protected]"<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; 
    } 

我知道這是錯誤的。我已經放置了檢查郵件ID的正則表達式。我只想 但我不知道正則表達式。

什麼應該是正確的方法?

在此先感謝您分享您的知識。

回答

1

這裏是一個RFC 2822兼容的正則表達式

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

或者使用一個

\b[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b 
1

葉。我做的。

NSString *[email protected]"<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); 
+0

如果它對你有效接受答案 – streetparade 2010-01-22 21:35:44

+0

你能接受你自己的答案嗎?我不認爲他用你的回覆... – 2010-01-22 22:36:21

+0

@肯代爾是的,你可以 - 48小時後。 http://blog.stackoverflow.com/2009/01/accept-your-own-answers/ – Amarghosh 2010-01-23 06:12:06

相關問題