0

这是示例:

<aNodeName thsisjijdsnjdnjsd>, 我想删除thsisjijdsnjdnjsd,

如何检测空格之前>和之后的字符串,并在目标 C 中将其修剪掉?另外,请提醒我不知道aNodeNameor thsisjijdsnjdnjsd,因为数据可能会变成这样:

<anotherNodeName zxzxxzxzxz>, 我需要删除zxzxxzxzxz.

4

1 回答 1

1

基本上你有两个选择

  • 常用表达

    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);
    }];
    

    请注意,您不应该使用正则表达式解析复杂的 html。

  • 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:@""]);
    

完整的基于命令行的示例

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

输出:

Regex approach: aNodee  thsisjijdsnjdnjsd
Regex approach: <aNodee> 
Scanner approach:  thsisjijdsnjdnjsd
Scanner approach: <aNodee>
于 2012-10-18T02:29:39.703 回答