我有一些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);
}];
它给出了结果,但有些时候它没有给出所有匹配的结果。 有人可以帮我解决我在这里做错了什么。
答案 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