0

For one NSString, I have 2 pattern strings. I want to extract substrings which match the pattern. The string format is like "@Realname![username]" and I want to separate Realname and username and then put these two strings in an array.

EDIT: I'd also like to search this particular substring from a bigger string which will be like "just about any @Realname![username] text which can be typed blah blah..."

Also, There can be as many substrings in the string of the same format and the string can have any characters including "@", "!", "[" or "]"

Sample string: "just about any @Realname1![username1] text which can be @Realname2![username2] typed blah blah..."

Anuj Rajput
  • 836
  • 9
  • 30

6 Answers6

0

well you can use componentsSeparatedByString: method of NSString class

freelancer
  • 1,658
  • 1
  • 16
  • 37
0

Find your start point:

[string rangeOfString:@"@"].location

Use this code snippet:

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

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

NSString *realname = [comp.firstObject substringFromIndex:1];
NSString *username = [comp.lastObject substringToIndex:name.length -1];
iCode
  • 1,456
  • 1
  • 15
  • 26
0

I'd say that componentsSeparatedByString: is the easiest approach, however, more generic (and correct, IMO) is to use 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];

....
kovpas
  • 9,553
  • 6
  • 40
  • 44
0

There are lots of NSString methods that would let you build this function.

You could build it with rangeOfString:

You'd find the range of the beginning "@" and "!" for the first string, and take the substring between those 2. Then you'd find the range of "[" and of "]" and take the substring between those 2.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

Try this,

-(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;
}

ans use,

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

NSString * userName = [self stringBetweenString:@"[" andString:@"]" inString:yourString];
NSLog(@"%@", userName);
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
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:@"@"];
    }
Nikhil Lihla
  • 607
  • 6
  • 21
  • This works perfectly for multiple occurrences of the token. Just need to append the first substring to the final string – Anuj Rajput Feb 19 '14 at 07:28