Question

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);
Was it helpful?

Solution

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&
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top