如何在目标c中通过正则表达式过滤字符串

时间:2014-12-11 08:06:46

标签: objective-c

假设有一个字符串

NSString *str = @"https://www.xxx.com?t=aaa&m=bbb&s=92,"

现在我想得到t,m,s的值如下:

aaa,bbb,92

谁能告诉我如何在对象c中实现它?

2 个答案:

答案 0 :(得分:0)

简单的方法就是

NSString *str = @"https://www.xxx.com?t=aaa&m=bbb&s=92,";

NSArray * list = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"?=&,"]];


//Test result    
for (int i = 1; (i+1)<[list count];i = i +2)
{
    NSLog(@"%@ = %@",[list objectAtIndex:i],[list objectAtIndex:(i+1)]);
}

答案 1 :(得分:0)

试试这样: -

    NSString *str1 = @"https://www.xxx.com?t=aaa&m=bbb&s=92,";
    NSMutableArray *arr1=[[str1 componentsSeparatedByString:@"="]mutableCopy];
    [arr1 removeObjectAtIndex:0];
    NSMutableArray *mutArray=[NSMutableArray array];
    for (NSString *str1 in arr1)
    {
        ([str1 length] >3)? [mutArray addObject:[str1 substringToIndex:3]] : [mutArray addObject:[str1 substringToIndex:2]];
    }
    NSLog(@"%@",[mutArray componentsJoinedByString:@","]);