正则表达式,查找字符串,数字和白色\换行符空间的模式

时间:2016-03-02 16:59:24

标签: ios objective-c regex nsregularexpression

我有一些html标签,我正在寻找一个特定的模式 - 它应该是“后续”,后面跟着空格,冒号或新行,后跟8到13位之间的数字。 / p>

我在正则表达式下方使用以找出此模式

NSString * followUp = @“后续行动614233222后续行动请在跟进标签中包含以下行,其中后续行动\ n:123212323 567后续行动1234231234,需要忽略123< \ n>请在此请求的后续电子邮件中包含以下行。< \ 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);
                             }];

它给出了结果,但有些时候它没有给出所有匹配的结果。 有人可以帮我解决我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

试试这个

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