使用NSString中的多个模式提取多个子字符串

时间:2014-02-06 12:04:43

标签: ios iphone objective-c nsstring

对于一个NSString,我有2个模式字符串。我想提取与模式匹配的子串。字符串格式类似于"@Realname![username]",我想将Realnameusername分开,然后将这两个字符串放在一个数组中。

编辑:我还想从更大的字符串中搜索这个特定的子字符串,就像"just about any @Realname![username] text which can be typed blah blah..."

一样

此外,相同格式的字符串中可以有多个子字符串,字符串可以包含任何字符,包括“@”,“!”,“[”或“]”

示例字符串:"just about any @Realname1![username1] text which can be @Realname2![username2] typed blah blah..."

6 个答案:

答案 0 :(得分:0)

你可以使用 componentsSeparatedByString: NSString类的方法

答案 1 :(得分:0)

找到你的起点:

[string rangeOfString:@"@"].location

使用此代码段:

NSString *name = @"@Realname![username]";

NSArray *comp = [name componentsSeparatedByString:@"!["];

NSString *realname = [comp.firstObject substringFromIndex:1];
NSString *username = [comp.lastObject substringToIndex:name.length -1];

答案 2 :(得分:0)

我会说componentsSeparatedByString:是最简单的方法,然而,更通用(和正确的,IMO)是使用NSRegularExpresison

....

NSString *string = @"@Realname![username]";
NSRegularExpression *nameExpression = [NSRegularExpression regularExpressionWithPattern:@"@([^\\!]+)\\!\\[([^\\]]+)\\]" options:NSRegularExpressionSearch error:nil];

NSArray *matches = [nameExpression matchesInString: string
                                  options:0
                                    range:NSMakeRange(0, [string length])];
NSTextCheckingResult *nameMatch = [matches firstObject];
NSRange realNameMatchRange = [nameMatch rangeAtIndex: 1];
NSRange usernameMatchRange = [nameMatch rangeAtIndex: 2];

NSString *realName = [string substringWithRange: realNameMatchRange];
NSString *username = [string substringWithRange: usernameMatchRange];

....

答案 3 :(得分:0)

有许多NSString方法可以让你构建这个功能。

您可以使用rangeOfString构建它:

你会找到开头“@”和“!”的范围。对于第一个字符串,并获取那些之间的子字符串2.然后你会找到“[”和“]”的范围,并在那些 2之间取代子字符串。

答案 4 :(得分:0)

试试这个,

-(NSString*)stringBetweenString:(NSString*)start andString:(NSString*)end inString:(NSString *)text{

    NSScanner* scanner = [NSScanner scannerWithString:text];
    [scanner setCharactersToBeSkipped:nil];
    if ([scanner scanString:start intoString:NULL]) {
        NSString* result = nil;
        if ([scanner scanUpToString:end intoString:&result]) {
            return result;
        }
    }
    return nil;
}

并使用,

NSString * str = [self stringBetweenString:@"@" andString:@"!" inString:yourString];
NSLog(@"%@", str);

NSString * userName = [self stringBetweenString:@"[" andString:@"]" inString:yourString];
NSLog(@"%@", userName);

答案 5 :(得分:0)

 NSString* str = @"just about any @Realname![username] text which can be typed blah blah...@Realname![username]text which can be typed blah blah...";
    NSRange segmentRange = [str rangeOfString:@"@"];
    while (segmentRange.location != NSNotFound)
    {
        NSRange endRange = [str rangeOfString:@"!"];
        NSString* value = [str substringWithRange:NSMakeRange(segmentRange.location + [@"@" length], endRange.location -(segmentRange.location + [@"@" length]))];
        NSString* realName = value;
        NSLog(@"real String : %@", realName);
        NSRange startRange1 = [str rangeOfString:@"["];
        NSRange endRange1 = [str rangeOfString:@"]"];
        NSString* secondValue = [str substringWithRange:NSMakeRange(startRange1.location + [@"[" length], endRange1.location -(startRange1.location + [@"[" length]))];
        NSString* userName = secondValue ;
        NSLog(@"user Name : %@", userName);
        str = [str substringFromIndex:endRange1.location+1 ];
        NSLog(@"the new string : %@", str);

        segmentRange = [str rangeOfString:@"@"];
    }