0

サーバーから次のような文字列を取得するたびに:

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

しかし、時々私は得るでしょう

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

これらのメッセージを破棄したい (「&」で終わらない) 例: &abc;123:342:431. 私は試しましたが、どこで間違いを犯しているのかわかりません。

これが私のコードです:

- (BOOL)ashCheckandsemiclonCheck:(NSString *)string {
    if([string rangeOfString:@"&abc"].location != NSNotFound) {
        NSLog(@"is here");
        NSString *pattern = @"&abc[^&]+&";
        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];
                                 matched = [string substringWithRange:range];

                                 NSLog(@"%@", matched);
                             }];
        NSLog(@"filtered Message :%@", matched);
        int times = [[matched componentsSeparatedByString:@"&"] count] - 1;
        NSLog(@"counted times :%i", times);

        if (times == 2) {
            return TRUE;
        } else {
            return FALSE;
        }       
    } else {   
        return FALSE;
    }
}

2 つある場合はこの引数を呼び出したいのです&が、私のコードでは error が発生しますNSLog(@"is here");。私が時々言ったように、私は半分のメッセージを受け取ります。誰が私が間違っているのか教えてもらえますか?

4

1 に答える 1

1

私があなたの質問を正しく読んでいれば、メッセージ文字列が「&」で始まって終わっていることを確認して、その文字列が有効かどうかをテストする必要があるようです。私がそれを行う方法は、次のようなものを書くことです:

 NSString* testMessage = @"&bfjhfbgfjhb&";
 NSString* firstCharacter = [testMessage substringToIndex:1];
 NSString* lastCharacter  = [testMessage substringFromIndex:testMessage.length - 1];

 if (([firstCharacter isEqualToString:@"&"]) &&
      [lastCharacter isEqualToString:@"&"]){

    //This is a valid message. Proceed.

 }else{

   //This is not a valid message. Discard.

 }

アップデート

わかりました、あなたの質問の編集/更新に基づいて、私はあなたのコード、特にあなたの正規表現パターン文字列を編集してメッセージの「&abc ...」サブセクションが始まることを確認することです。 「&」文字で終わります。

-(BOOL) ashCheckandsemiclonCheck :(NSString *)string;
{

    if([string rangeOfString:@"&abc"].location !=NSNotFound)
    {
        NSLog(@"is here");

        NSString *pattern = @"&abc[^&\\s]+[^\\s]";

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

                             NSLog(@"Matched: %@", matched);

                         }];

        NSLog(@"filtered Message :%@", matched);
        int times =[[matched componentsSeparatedByString:@"&"]count]-1;
        NSLog(@"counted times :%i",times);

        if(times ==2){
            return YES;
        }else  
        {
            return NO;
        }       
    }
    else
    {   
        return NO;
    }
}

また、BOOL の戻り値が TRUE と FALSE ではなく、Objective-C 標準の YES と NO に変更されました。

それがあなたが必要とすることを願っています。

于 2013-04-17T14:47:34.350 に答える