先行(?=
)是错在这里,你有没有正确地躲过了\d
(变成\\d
)和最后但并非最不重要,你离开了量词*
(0次或更多次),并+
(1次以上):
NSString *aTestString = @"[email protected]#[email protected]#[email protected]#$**888***";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"value=[^\\d]*(\\d+)"
options:0
error:NULL
];
[regex
enumerateMatchesInString:aTestString
options:0
range:NSMakeRange(0, [aTestString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(@"Value: %@", [aTestString substringWithRange:[result rangeAtIndex:1]]);
}
];
编辑:这里的一个更精细的图案。它在=
之前捕获一个单词,然后丢弃非数字并在之后捕获数字。
NSString *aTestString = @"[email protected]#[email protected]#[email protected]#$**888***";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\w+)=[^\\d]*(\\d+)" options:0 error:NULL];
[regex
enumerateMatchesInString:aTestString
options:0
range:NSMakeRange(0, [aTestString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(
@"Found: %@=%@",
[aTestString substringWithRange:[result rangeAtIndex:1]],
[aTestString substringWithRange:[result rangeAtIndex:2]]
);
}
];
// Output:
// Found: foo=777
// Found: bar=888
嘿,你是对的。我只是想出了您的编辑根据您的第一个帮助和即将发表评论:) 一个件事寿,我将离开图案(值=)[^ \\ d *(\\ d +),因为“值=“始终有保证。非常感谢您的回答。将其标记为正确。 –