简体   繁体   中英

Regular Expression,Find pattern of String ,Number and white\newline spaces

I have string of html tags in which I am looking for a particular pattern- It should be "Follow-up" which can be followed by space ,colon character or new lines ,followed by number between 8 and 13 digit.

I am using below Regular Expression to find out this pattern

NSString *followUp = @" Follow-up 614233222 Follow-up Please inlcude the below line in follow up tags where Follow-Up \\n:123212323 567 Follow-Up 1234231234 which needs to be ignore with 123 <\\n>Please include the line below in follow-up emails for this request.<\\n><\\n>";

    NSString *pattern = [NSString stringWithFormat:@"Follow-up(\\s|:)*\\d{8,13}"];

    NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:NULL];

    [regExp enumerateMatchesInString:followUp
                                options:0
                                  range:NSMakeRange(0, followUp.length)
                             usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
                                 NSString * foundMatch = [followUp substringWithRange:[result rangeAtIndex:0]];
                                 NSLog(@"found is %@",foundMatch);
                             }];

It is giving result but some time it is not giving all matching results. Can someone please help me what I am doing wrong here.

Try this

Follow-up[\\s\\n\\\\n:]+[\\d]{8,13}

Regex101 Demo

NSString *pattern = [NSString stringWithFormat:@"Follow-up[\\s\\n\\\\n:]+([\\d]{8,13})"];
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
[regExp enumerateMatchesInString:followUp
                         options:0
                           range:NSMakeRange(0, followUp.length)
                      usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
                          NSString * foundMatch = [followUp substringWithRange:[result rangeAtIndex:1]];
                          NSLog(@"found is %@",foundMatch);

NSLog:

found is 614233222
found is 123212323
found is 1234231234

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM