2013-04-17 9 views
6

これはサンプルのNSString(配列に解析されたもの)です。objective-cの解析されたNSStringから単一の値を抽出する

単一文字列オブジェクトは次のように解析され、 "STRING1、STRING2その後、DATEのTIMEそして - A:VALUE"、EX:

The name of first value, first 17/04/2013 11:30:00 - A:30.0 // sometimes there is a comma after first string 
The name of second value 17/04/2013 11:00:00 - A:20.0 // sometimes there is NOT a comma after first string 
Name of third value 17/04/2013 10:30:00 - A:40.0 
Fourth 17/04/2013 09:40:00 - A:50.0 
Fifth value 17/04/2013 05:00:00 - A:10.0 

まあ、私はSTRINGまたはSTRING、STRINGの別々の値を抽出する必要があります。

The name of first value, first 
The name of second value 
Name of third value 
Fourth 
Fifth value 

その後、DATEおよびTIME最後に

17/04/2013 11:30:00 
17/04/2013 11:00:00 
17/04/2013 10:30:00 
17/04/2013 09:40:00 
17/04/2013 05:00:00 

、単一VALUES:

30.0 
20.0 
40.0 
50.0 
10.0 

EDIT:私は解析された* allValuesのためのマーティンRからのコードを使用しようとしている:

The *allValues array is something like: 
    (
     "The name of first value, first 17/04/2013 11:30:00 - A:30.0", 
     "The name of second value 17/04/2013 11:00:00 - A:20.0", 
     "Name of third value 17/04/2013 10:30:00 - A:40.0", 
     "Fourth 17/04/2013 09:40:00 - A:50.0", 
     "Fifth value 17/04/2013 05:00:00 - A:10.0" 
    ) 

、ここでは、単一の値を抽出するためのコードです:

NSMutableArray *allValues = [parsedFeed valueForKey:@"values"]; 

for (int i=1; i < [allValues count]; i++) { 
    NSString *string = [allValues objectAtIndex:i]; 
    NSString *pattern = @"(.*) (\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}) - A:(.*)"; 
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL]; 
    NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])]; 

    if (match != nil) { 
    NSString *s1 = [string substringWithRange:[match rangeAtIndex:1]]; 
    NSString *s2 = [string substringWithRange:[match rangeAtIndex:2]]; 
    NSString *s3 = [string substringWithRange:[match rangeAtIndex:3]]; 
    NSLog(@"First part: %@", s1); 
    NSLog(@"Second part: %@", s2); 
    NSLog(@"Third part: %@", s3); 
    } 
} 

しかし、この場合、私はNSLogの結果を得ることができません(一致== nil)。何が起こったか?ありがとう!

+0

を[あなたは何を試してみました?](のhttp:// whathaveyoutried。 com)私たちはあなたの問題を解決するためではなく、問題を解決しようとしている特定の問題を手伝うためにここにはいません。また、[よくある質問](http://stackoverflow.com/questions/how-to-ask) –

答えて

12

DATE/TIMEは固定フォーマットを持っている場合は、正規表現を使用することができます

NSString *string = @"The name of first value, first 17/04/2013 11:30:00 - A:30.0"; 
NSString *pattern = @"(.*) (\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}) - A:(.*)"; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern 
             options:0 error:NULL]; 
NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])]; 
if (match != nil) { 
    NSString *s1 = [string substringWithRange:[match rangeAtIndex:1]]; 
    NSString *s2 = [string substringWithRange:[match rangeAtIndex:2]]; 
    NSString *s3 = [string substringWithRange:[match rangeAtIndex:3]]; 
    NSLog(@"First part: %@", s1); 
    NSLog(@"Second part: %@", s2); 
    NSLog(@"Third part: %@", s3); 
} 

が出力:

 
First part: The name of first value, first 
Second part: 17/04/2013 11:30:00 
Third part: 30.0 
+0

ありがとうございます@マーティンR、あなたのコードは1つの固定文字列に最適ですが、あなたは私の質問に編集を参照してください、私はNSMutableArrayの文字列から値を抽出するために使用することはできません...いくつかの問題はありますか? – Huxley

+0

@Huxley:あなたのループは0ではなくインデックス1で始まり、最初の文字列は時間フィールドでコロン(:)ではなくドット(。)を使用することに注意してください。私の正規表現はコロンをチェックします。ドットやコロンをチェックするために一般化することができます。 - それ以外は、配列に問題は見られず、EDITEDコードはテストアプリで問題なく動作します。 –

+0

パーフェクト、マーティン、それはタイプミスでした! :)最後の倍精度浮動小数点、ドットやコロンのチェックを "一般化"する方法は何ですか?例えば、コードを提供できますか?ありがとうございました! – Huxley

-1

あなたが望むことを達成するためにNSStringクラスによって提供される多くの異なるメソッドがあります。 文字列と部分文字列の検索、結合、および分割。 これらのメソッドを見て、私はあなたが望む文字列を得るためにそれらを組み合わせることができると確信しています。私はif()else bracesが必要になると思います。

http://developer.apple.com/library/ios/#Documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

あなたはこれがあなたのお役に立てば幸いです。

関連する問題