0

UITextView では、ユーザーは{{1}}またはのような形式の数値を入力できます{{177}}。プレビュー フィールドでは、SO のように、そのパターンを NSArray の値に置き換えたいと思います。ここで、番号は配列内の行に対応します。

これを解決するには、次の 2 つのアイデアがあります。

  1. NSRegularExpression ですべての出現をカウントし、それらをループして置き換えます。
  2. NSString から単語配列を作成し、配列をループして出現箇所を置き換え、NSString を元に戻します。

これら 2 つのオプションのうち、パフォーマンスと信頼性が高いのはどれですか? 別の方法はありますか?

-- update -- これは、パターンを見つけるための正規表現です: @"\\{\\{([1-9]|[1-9][0-9]|[1-9][0-9][0-9])\\}\\}". パターンを置き換えるために整数を含むループでこれを使用するにはどうすればよいですか?

4

3 に答える 3

2

を使用した回答は次のNSRegularExpressionとおりです。

NSString * input = @"{{83}} fjsdkfjds {{122}}";

NSMutableString * strToMakeReplacements = [[NSMutableString alloc] initWithString:input];

NSError * error = nil;
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{([1-9]|[1-9][0-9]|[1-9][0-9][0-9])\\}\\}" options:NSRegularExpressionCaseInsensitive    error:&error];

NSArray * matches = [regex matchesInString:input options:NSMatchingReportProgress range:NSMakeRange(0, [input length])];

for (int i = [matches count]-1; i>=0 ; i--) {
    NSTextCheckingResult * match = [matches objectAtIndex:i];

    NSRange matchRange = match.range;
    NSString * numString = [input substringWithRange:NSMakeRange(matchRange.location+2, matchRange.length-4)];
    NSInteger num = [numString intValue];

    NSString * replacementValue = [self replacementValueForNum:num]; // write this function yourself

    [strToMakeReplacements replaceCharactersInRange:match.range withString:replacementValue];
}

正規表現のスラッシュをエスケープする必要があることに注意してください。\\{\\{([1-9]|[1-9][0-9]|[1-9][0-9][0-9])\\}\\}

于 2011-07-06T16:42:20.707 に答える
1

正規表現を使用するよりも便利なように思われるので、2番目のアプローチを使用します(確かに、それらについてはあまり経験がありませんが)。

入力文字列が与えられた場合、NSString* input次のように数字ではない文字を除外します。

NSCharacterSet* notNumbersSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSArray * wholeNumbers = [input componentsSeparatedByCharactersInSet:notNumbersSet];

次に、整数の配列をループします。

for( NSString* numString in wholeNumbers ) {
    NSInteger num = [numString intValue];
    // ...
}
于 2011-07-06T13:03:47.423 に答える
1

アプローチは次のNSRegularExpressionようなものになります–

NSRegularExpression * expression = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{(\\d+?)\\}\\}"
                                                                             options:0
                                                                               error:nil];
NSMutableString * resultString = [aString mutableCopy];
NSTextCheckingResult * result;
NSUInteger location = 0;

/* Get the first match in the remaining string to search */
while ((result = [expression firstMatchInString:resultString options:0 range:NSMakeRange(location, [resultString length] - location)])) {

    /* Get the index of the array using the capture group 1 i.e. (\\d+?) */
    NSUInteger index = [[resultString substringWithRange:[result rangeAtIndex:1]] integerValue];

    /* Get the string that will replace the match */
    NSString * replacementString = [replacementStrings objectAtIndex:index];

    /* Replace the string */
    [resultString replaceCharactersInRange:result.range withString:replacementString];

    /* Skip to the end of the replaced string */
    location = result.range.location + [replacementString length];
}

置換文字列は配列から取得されるため、これに対する単一のステートメント ソリューションがあるとは思えません。ここで一致を見つけ、それを適切に置き換えてから、残りの文字列で一致を検索します。これを繰り返しますが、これ以上の結果はありません。

于 2011-07-06T16:42:50.610 に答える