ここでは先読み ( ?=
) が間違っています。\d
( になる\\d
) を正しくエスケープしていませんが、量指定子*
(0 回以上) と+
(1回以上) を省略しています。
NSString *aTestString = @"value=!@#777!@#value=@#$**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 = @"foo=!@#777!@#bar=@#$**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