ios - How do I extract a list of email/mailbox strings within text or test if a string is a proper email address? -
Looking at some arbitrary text, I get all the email addresses and the 'mailbox specifier' (like The way to access it is a very good algorithm that can identify as many valid addresses, and improper people Can reject Perhaps the best solution would be a parser using Lex and Yake, but there are proper solutions using regular expressions. A list of regularly tested expressions, along with a more in-depth discussion of the problem and possible solutions. Regular expressions to be displayed on the above site are formatted for PHP, and are forward and trailing '/' markers, as well as signal-insensitive such as 'flags' etc. (more to see this Information), so this objective should be stripped before using the expression in the project. Also, it is necessary to separate any anchors too, because we do not want many addresses just one (i.e. '' '' 'and' $ '). NSRGular Expression is the class to use here, what I have found is to store regular expressions in a file in my project, so that you have to worry about avoiding all backslashes and quotes Not required. The code then reads the expression in a string, and makes the object as follows: Once you receive an initial expression, you use it to return a list of categories, each limit is an address: However, we know that all email addresses have a '@', so to verify it, it is worthwhile to verify that string processing is at least one. Apart from this, because the text and / or the carriage can return the text, so you want to strip those first bars, perhaps it is better to strip them because some mail programs split a line at some internal point of address Can do. Once you have a list of address categories, then the work is done for the most part - you should know if you want. However, addresses are often presented in the "mailbox specifier" format, where a name is worn at the address, and the address is wrapped with '& lt;' and '& gt;' This format is included in section 3.4. To recover a name from a 'mailbox specifier', check to see that the address '& lt;' And '& gt;', and if so, 'end lieutenant;' Find the string before that, ignoring the white space (unless you can not find the first letter). Most names will be wrapped in double quotes (normal behavior), but can actually be nude white space or other special characters (Eg '' '), alphanumeric strings to avoid backslash. This same technique can be used for real-time verification - one tone EX String To enable a submit button when a valid email address is created, in this case you can evaluate the string on each user change, and enable / disable the submit button. If It all seems like a lot of code work, so you can catch an open source project. EDIT1: For a fast but less rigid method, see comments by Kodafe. EDIT2: This one "Content of mailto shows: URL can be quite complex, Gitub project handles only the easiest, and does not un-encode the address. This will be addressed in future updates. EDIT3: decode the project "Melo: Objects" completely and CC, BCC, Subject, and Body, all URLs. "Fred Smith" Lt; fred@me.com> ). I looked at NSDataDetector, but it does not handle the email address.
NSString * fullPath = [[NSBundle main bundle] pathForResource: self.regex ofType: @ "txt"]; NSString * pattern = [NSString stringWithContentsOfFile: Fulltext Encoding: NSUTF8StringEncoding Error: NULL]; __autoreleasing NSError * Error = zero; Reg = [NSRGular Expression Regular Experimental Pattern: Pattern Options: NSRGularExpressCinsensitiveError: & amp; Adidas;]; // Some patterns may not be required; NSRGular ExpressionInsensitive Attacht (Reg & Au; Emmetti);
NSArray * ret = [reg MatchesInString: str Option: 0 Category: NSMakeRange (0, [str length]) ;; Although
Comments
Post a Comment