0

GPS 座標文字列からさまざまなコンポーネントを抽出する必要があります。たとえば、次のようになります。

+30° 18' 12" N  // pull out 30, 18 & 12

また

+10° 11' 1" E    // pull out 10, 11 & 1

また

-3° 1' 2" S    // pull out -3, 1 & 2

また

-7° 12' 2" W    // pull out -7, 12 & 2

私はオンラインで見回しており、そこにあることに気づきましたNSRegularExpression。これを何らかの方法で使用することは可能かどうか疑問に思っていましたか?提供されたドキュメントも見て、さまざまな部分を引き出すために正規表現をまとめようとしました。これは私が思いついたものです:

('+'|'-')$n°\s$n'\s$n"\s(N|E|S|W)

これが正しいかどうかはよくわかりません。チュートリアルや例があまりないため、使用方法も不明です。誰か助けてくれませんか?使用するのではなく、これを行うためのより良い方法がある場合、NSRegularExpression私はそれを受け入れますが、私が知る限り、目的の c には組み込みの正規表現サポートがありません。

4

5 に答える 5

4

正規表現はやり過ぎです、私見。[NSString componentsSeparatedByString:]文字列を部分に分割し[NSString intValue]、最後のコンポーネントを除く各コンポーネントの数値をからかうために、区切り文字としてスペースを使用します。

于 2012-01-23T21:00:35.427 に答える
2

REのやり過ぎ(セヴァ)?オブジェクトはどうですか?;-)

NSString *coords = @"+30° 18' 12\" N";

int deg, sec, min;
char dir;

if(sscanf([coords UTF8String], "%d° %d' %d\" %c", &deg, &min, &sec, &dir) != 4)
   NSLog(@"Bad format: %@\n", coords);
else
   NSLog(@"Parsed %d deg, %d min, %d sec, dir %c\n", deg, min, sec, dir);

これが好きかどうかは、Cにドロップするというあなたの見方によって異なりますが、それは直接的で単純です。

于 2012-01-23T21:20:40.167 に答える
2

NSScanner の使用:

NSScanner *scanner;
NSCharacterSet *numbersSet = [NSCharacterSet characterSetWithCharactersInString:@" °'"];
int degrees;
int minutes;
int seconds;

NSString *string = @" -7° 12' 2\" W";
scanner = [NSScanner scannerWithString:string];
[scanner setCharactersToBeSkipped:numbersSet];
[scanner scanInt:&degrees];
[scanner scanInt:&minutes];
[scanner scanInt:&seconds];
NSLog(@"degrees: %i, minutes: %i, seconds: %i", degrees, minutes, seconds);

NSLog 出力:

degrees: -7, minutes: 12, seconds: 2
于 2012-01-23T21:53:51.357 に答える
0

必要なものは次のとおりです。@"([+-]?[0-9]+)"

コード例は次のとおりです。

NSString *string;
NSString *pattern;
NSRegularExpression *regex;
NSArray *matches;

pattern = @"([+-]?[0-9]+)";

regex = [NSRegularExpression
         regularExpressionWithPattern:pattern
         options:NSRegularExpressionCaseInsensitive
         error:nil];

string = @" -7° 12' 2\" W";
NSLog(@"%@", string);
matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
degrees = [[string substringWithRange:[[matches objectAtIndex:0] range]] intValue];
minutes = [[string substringWithRange:[[matches objectAtIndex:1] range]] intValue];
seconds = [[string substringWithRange:[[matches objectAtIndex:2] range]] intValue];
NSLog(@"degrees: %i, minutes: %i, seconds: %i", degrees, minutes, seconds);

NSLog出力:

度:-7、分:12、秒:2

于 2012-01-23T21:31:09.083 に答える
0
NSMutableArray *newCoords = [[NSMutableArray alloc] init];
NSArray *t = [oldCoords componentsSeparatedByString: @" "];

[newCoords addObject: [[t objectAtIndex: 0] intValue];
[newCoords addObject: [[t objectAtIndex: 1] intValue];
[newCoords addObject: [[t objectAtIndex: 2] intValue];

の投稿で与えられた座標を持っていると仮定すると、NSString oldCoords必要な 3 つのデータを含む がNSMutableArray呼び出されます。newCoords

于 2012-01-23T21:05:37.487 に答える