0

Here is the example:

<aNodeName thsisjijdsnjdnjsd>, and I would like to remove thsisjijdsnjdnjsd,

How can I detect the string which is before the > and after the space , and trim out it in objective C? Also, please remind that I don't know the aNodeName or thsisjijdsnjdnjsd, because the data may turn out something like this:

<anotherNodeName zxzxxzxzxz>, and I need to remove zxzxxzxzxz.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags - trust me on this – Kate Gregory Oct 19 '12 at 21:56
  • @KateGregory — we dont know, if this is meant for html parsing. But note, that I added the link you provided as warning in my answer, in case it is meant for html-parsing. – vikingosegundo Oct 20 '12 at 16:38

1 Answers1

1

Basically you have two options

  • Regular expressions

    NSString *string = @"<aNodee thsisjijdsnjdnjsd>";
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(\\S+)( .*)>" options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    [matches enumerateObjectsUsingBlock:^(NSTextCheckingResult *result, NSUInteger idx, BOOL *stop) {
        NSString* nodeName = [string substringWithRange:[result rangeAtIndex:1]];
        NSString* value = [string substringWithRange:[result rangeAtIndex:2]];
        NSLog(@"%@ %@",nodeName, value);
    }];
    

    Note, that you shouldn't parse complex html with Regular Expressions.

  • NSScanner

    NSScanner *scanner = [NSScanner scannerWithString:string];
    BOOL recordingValue = NO;
    
    NSMutableString *valueString = [@"" mutableCopy];
    [scanner setScanLocation:0];
    while (![scanner isAtEnd]) {
    
        NSString *charAtlocation = [string substringWithRange:NSMakeRange([scanner scanLocation], 1)];
        if ([charAtlocation isEqualToString:@" "]){
            recordingValue = YES;
            [valueString appendString:@" "];
        } else{
            if ([charAtlocation isEqualToString:@">"]){
                recordingValue = NO;
            } else if (recordingValue) {
                [valueString appendString:charAtlocation];
            }
        }
        [scanner setScanLocation:[scanner scanLocation]+1];
    } ;
    
    
    NSLog(@"Scanner approach: %@", valueString);
    NSLog(@"Scanner approach: %@", [string stringByReplacingOccurrencesOfString:valueString withString:@""]);
    

Complete command line based example

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSString *string = @"<aNodee thsisjijdsnjdnjsd> ";
        NSError *error;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<([a-zA-z]+)( .*)>" options:NSRegularExpressionCaseInsensitive
                                                                                 error:&error];
        NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
        [matches enumerateObjectsUsingBlock:^(NSTextCheckingResult *result, NSUInteger idx, BOOL *stop) {
            NSString* nodeName = [string substringWithRange:[result rangeAtIndex:1]];
            NSString* value = [string substringWithRange:[result rangeAtIndex:2]];
            NSLog(@"Regex approach: %@ %@",nodeName, value);

            NSLog(@"Regex approach: %@", [string stringByReplacingOccurrencesOfString:value withString:@""]);
        }];




        NSScanner *scanner = [NSScanner scannerWithString:string];
        BOOL recordingValue = NO;

        NSMutableString *valueString = [@"" mutableCopy];
        [scanner setScanLocation:0];
        while (![scanner isAtEnd]) {

            NSString *charAtlocation = [string substringWithRange:NSMakeRange([scanner scanLocation], 1)];
            if ([charAtlocation isEqualToString:@" "]){
                recordingValue = YES;
                [valueString appendString:@" "];
            } else{
                if ([charAtlocation isEqualToString:@">"]){
                    recordingValue = NO;
                } else if (recordingValue) {
                    [valueString appendString:charAtlocation];
                }
            }
            [scanner setScanLocation:[scanner scanLocation]+1];
        } ;


        NSLog(@"Scanner approach: %@", valueString);
        NSLog(@"Scanner approach: %@", [string stringByReplacingOccurrencesOfString:valueString withString:@""]);
    }
    return 0;
}

Output:

Regex approach: aNodee  thsisjijdsnjdnjsd
Regex approach: <aNodee> 
Scanner approach:  thsisjijdsnjdnjsd
Scanner approach: <aNodee>
Community
  • 1
  • 1
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178