0

I appoligize for the same question but i didnt got the answer i was trying for 2 days m not getting it , can any one help me .

i need to find and print only the string which starts and ends with "&". but in my string lots of same occurance of "&"

EX:

&abc;123:342:431:234& &xyz;232:2344:433:434& &pqr;234:453:534:3445&

i want to print only string starts with "&xyz" and then ends with "&" (&xyz;232:2344:433:434&)

sometime i ll get
&pqr;234:453:534:3445& &abc;123:342:431:234& &xyz;232:2344:433:434&

here also i want to print the same "&xyz" and ends with "&".

I tried ," NSRange", "NSscanner", "NSpredictive" . But i dont get that the specific string to be ptrit

// NSScanner :

    NSMutableArray *substrings = [NSMutableArray new];
    NSScanner *scanner = [NSScanner scannerWithString:s];
    [scanner scanUpToString:@"&abc" intoString:nil]; //
    NSString *substring = nil;
    [scanner scanString:@"&abc" intoString:nil]; // Scan the # character
    if([scanner scanUpToString:@"&" intoString:&substring]) {
       // If the space immediately followed the &, this will be skipped
       [substrings addObject:substring];
       NSLog(@"substring is :%@",substring);
    }
    // do something with substrings
    [substrings release];


// NSpredictive :

NSString *new=@"&abc;123:342:431:234& &xyz;232:2344:433:434& &pqr;234:453:534:3445&";

NSArray *arr = [new ComponentsSepetratedByString @:" "];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@ AND self ENDSWITH[cd] %@",@"&abc",@"&"];

NSLog(@"Sorted Array %@",[arr filteredArrayUsingPredicate:predicate]);
NSArray *sortedArray = [arr filteredArrayUsingPredicate:predicate];

NSMutableArray *finalResult = [NSMutableArray arrayWithCapacity:0];
for(NSString *string in sortedArray)
{
    NSString *content = string;
    NSRange range1 = [content rangeOfString:@"&abc"];
    if(range1.length > 0)
        content = [content stringByReplacingCharactersInRange:range1 withString:@""];

    NSRange range2 = [content rangeOfString:@"&"];
    if(range2.length > 0)
        content = [content stringByReplacingCharactersInRange:range2 withString:@""];

    [finalResult addObject:content];
}

NSLog(@"%@",finalResult);
Diaaaaan
  • 35
  • 6
  • you can try using regular expression – iAppDeveloper Apr 15 '13 at 07:19
  • i tried but i am getting printed from (&xyz to last occurance of "&") but i need only specific string to be printed – Diaaaaan Apr 15 '13 at 07:24
  • I tried NSArray *arr = [NSArray arrayWithObjects:@"&xyz;123:183:184:142&", @"&abc;134:534:435:432&", @"&qwe;323:535:234:532&", @"& I am not in it", @"&abc I am out &" ,nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@ AND self ENDSWITH[cd] %@",@"&abc",@"&"]; NSLog(@"%@",[arr filteredArrayUsingPredicate:predicate]); and it is giving the result you want – Raj Apr 15 '13 at 07:35

1 Answers1

0

Is this what you are looking for?

NSString *string = @"&abc;123:342:431:234& &xyz;232:2344:433:434& &pqr;234:453:534:3445&";

NSString *pattern = @"&xyz[^&]+&";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                       options:0 error:NULL];

[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length])
             usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                 NSRange range = [result range];
                 NSString *matched = [string substringWithRange:range];

                 NSLog(@"%@", matched);
             }];

Output:

&xyz;232:2344:433:434&
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382