您也可以使用正则表达式。
请注意,在该块中,我使用捕获块,通过[result rangeAtIndex:i]
.
索引 1 现在将是门牌号,索引 2 将返回第一条街道,索引 3 将返回第二条街道。
#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;
}
第二次编辑
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;
}