How to find emails in a given message and replace with XXXXX in iOS

Hi I am stuck with one situation on lines. My text looks like this

Electronic mail, commonly referred to as e-mail or e-mail since 1993 [2], is a way of exchanging digital messages from an author to one or more recipients. Modern e-mail works over the Internet or other computer networks. ex@ex.in Some early email systems required the author and recipient to be online at the same time, along with instant messages. Email systems today are based on the store-and-forward model. Email servers accept, forward, deliver and store messages. Neither users nor their computers should be connected at the same time; they should only connect briefly, usually to the mail server, as long as it is required to send or receive messages. some@email.comHistorically, the term "email" has been used generically for any transmission of an electronic document. esample@some.in For example, several authors in the early 1970s used the term to describe fax transmission. [3] [4] As a result, it is difficult to find the first quotation to use the term with the more specific meaning it has today.

there are 3 emails in the above text ( ex@ex.in , esample@some.in , some@email.com ), I have to find them and replace them with XXXXXXXXX for security reasons.

Any suggestions would be helpful

+3


source to share


4 answers


Use data detectors.

NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *matches = [linkDetector matchesInString:labelText options:0 range:NSMakeRange(0, [labelText length])];

      



It will find you URLs and email addresses, but it will be easier from now on.

+1


source


Designate a sentence (create an array of words). Then for each entry, if it matches some regex (or at least contains @), then find that entry in the sentence and replace it with XXXX.

(Typing on the phone, so don't remember the exact code).



//*** Pseudocode

//1. Split sentence into array of words
NSArray *words = [sentence componentsSeparatedByString:@" "];

for word in words
if word contains @"@" {
   sentence = [sentence stringByReplacingOccurencesOfString:word withString:@"XXX"];
}

      

0


source


You can achieve this by replacing Regex

NSString *string = @"Electronic mail, most commonly referred to as email or e-mail since c 1993,[2] is a method of exchanging digital messages from an author to one or more recipients. Modern email operates across the Internet or other computer networks. ex@ex.in Some early email systems required that the author and the recipient both be online at the same time, in common with instant messaging. Today email systems are based on a store-and-forward model. Email servers accept, forward, deliver, and store messages. Neither the users nor their computers are required to be online simultaneously; they need connect only briefly, typically to a mail server, for as long as it takes to send or receive messages. some@email.com Historically, the term electronic mail was used generically for any electronic document transmission. esample@some.in For example, several writers in the early 1970s used the term to describe fax document transmission.[3][4] As a result, it is difficult to find the first citation for the use of the term with the more specific meaning it has today.";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" options:NSRegularExpressionCaseInsensitive error:nil];

NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"XXXXXXXXX"];

      

0


source


Use the NSRegularExpression

using the method: replaceMatchesInString:

.

- (NSString *)parseString:(NSString *)original {
    NSMutableString *replacementString = [original mutableCopy];
    NSRange replacementRange = NSMakeRange(0, replacementString.length);

    NSString *pattern = @"\\S+@[^ .]+\\.[^ .?!]{2,}"";
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:pattern
                                  options:0
                                  error:nil];
    [regex replaceMatchesInString:replacementString options:0 range:replacementRange withTemplate:@"XXXXX"];

    return replacementString;
}

      

Concept: space non-space @ non-space. non-blank - - -. space
Note that email addresses can contain non-Latin characters, so A-za-z is not a good method.

0


source







All Articles