正規表現を使用することもできます。
ブロックでは、を介してキャプチャブロックを使用していることに注意してください[result rangeAtIndex:i]
。
インデックス1は家番号になり、インデックス2は最初の通りを返し、3は2番目の通りを返します。
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSArray *streets = [NSArray arrayWithObjects:@"100 - PARK STREET / JAMES PLACE", @"100 - BI-CENTENNIAL DRIVE / JAMES PLACE", @"100- BI-CENTENNIAL DRIVE / JAMES PLACE", @"100-BI-CENTENNIAL DRIVE /JAMES PLACE", nil];
NSString *text = [streets componentsJoinedByString:@" "];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d+) {0,1}- {0,1}(\\D+) *\\/ *(\\D+)" options:NSRegularExpressionCaseInsensitive error:nil];
[regex enumerateMatchesInString:text options:0
range:NSMakeRange(0, [text length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSLog(@"%@", [text substringWithRange:[result rangeAtIndex:i]]);
}
}];
}
return 0;
}
出力:
100
PARK STREET
JAMES PLACE
100
BI-CENTENNIAL DRIVE
JAMES PLACE
100
BI-CENTENNIAL DRIVE
JAMES PLACE
100
BI-CENTENNIAL DRIVE
JAMES PLACE
コメントに応じて編集する
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSArray *streets = [NSArray arrayWithObjects:@"100 - PARK STREET / JAMES PLACE", @"100 - BI-CENTENNIAL DRIVE / JAMES PLACE", @"100- BI-CENTENNIAL DRIVE / JAMES PLACE", @"100-BI-CENTENNIAL DRIVE /JAMES PLACE",@"100 - PARK STREET", nil];
NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:@"(\\d+) *- *([^\\/]+) *$" options:NSRegularExpressionCaseInsensitive error:nil];
NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@"(\\d+) *- *([^\\/]+) *\\/ *([^\\/]+) *$" options:NSRegularExpressionCaseInsensitive error:nil];
for (NSString *text in streets) {
NSRegularExpression *regex = ([regex1 numberOfMatchesInString:text options:NSRegularExpressionCaseInsensitive range:NSMakeRange(0, [text length])]) ? regex1 : regex2;
[regex enumerateMatchesInString:text options:0
range:NSMakeRange(0, [text length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSLog(@"%@", [text substringWithRange:[result rangeAtIndex:i]]);
}
}];
}
}
return 0;
}
2番目の編集
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSArray *streets = [NSArray arrayWithObjects: @"100 - PARK STREET / JAMES PLACE",
@"100 - BI-CENTENNIAL DRIVE / JAMES PLACE",
@"100- BI-CENTENNIAL DRIVE / JAMES PLACE",
@"100-BI-CENTENNIAL DRIVE /JAMES PLACE",
@"100 - PARK STREET",
@"100 - PARK STREET / ",
@"100 - PARK STREET/ ",
@"100 - PARK STREET/",
nil];
NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:@"(\\d+) *- *([^\\/]+) *$" options:NSRegularExpressionCaseInsensitive error:nil];
NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@"(\\d+) *- *([^\\/]+) *\\/ *([^\\/]*) *$" options:NSRegularExpressionCaseInsensitive error:nil];
for (NSString *text in streets) {
text= [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"\n>%@<", text);
NSRegularExpression *regex = ([regex1 numberOfMatchesInString:text options:NSRegularExpressionCaseInsensitive range:NSMakeRange(0, [text length])]) ? regex1 : regex2;
[regex enumerateMatchesInString:text options:0
range:NSMakeRange(0, [text length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSLog(@"%@", [text substringWithRange:[result rangeAtIndex:i]]);
}
}];
}
}
return 0;
}