【问题标题】:Regular Expression,Find pattern of String ,Number and white\newline spaces正则表达式,查找字符串、数字和空白\换行符的模式
【发布时间】:2016-03-03 01:28:29
【问题描述】:

我有一串 html 标签,我正在其中寻找特定模式 - 它应该是“后续”,后面可以跟空格、冒号字符或换行符,后面跟 8 到 13 位之间的数字。

我正在使用下面的正则表达式来找出这种模式

NSString *followUp = @" Follow-up 614233222 Follow-up 请在后续标签中包含以下行,其中 Follow-Up \n:123212323 567 Follow-Up 1234231234 需要忽略 123 请在此请求的后续电子邮件中包含以下行。";

    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);
                             }];

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

【问题讨论】:

    标签: ios objective-c regex nsregularexpression


    【解决方案1】:

    试试这个

    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
    

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 2019-07-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      相关资源
      最近更新 更多