iOS SDK メモ NSRegularExpressionSearchを使って文字列が数値か判定してみる

iPad,Mac,プログラミング

正規表現が使えるようになってたことを今更知っちゃった…。
今まで文字列が数字かどうか判定するのって、ガリガリコードを書いてたけど、これからは正規表現を使ってある程度スマートに書けるってことだな。

とりあえず、この条件の数値が判定できることを目標にしてみた。

  • 3桁ごとのカンマ区切りなし
  • 符号(+,-)あり/なし
  • 実数値

で、ポイントとなるのはNSStringのrangeOfStringってメソッド。
こういった引数を指定する。

  • NSString:rangeOfString:は正規表現(文字列)を指定
  • NSString:rangeOfString:options:は正規表現で検索するという意味のNSRegularExpressionSearchという値を指定

作った判定関数はこんなかんじ。
※テスト用にコンソール出力処理が入ってるけど、あってもなくてもOK。

Boolean IsNumbers(NSString* string)
{
	// 変数定義
	NSRange range;
	Boolean result = false;
	NSString* expression = @"^[-|+|[.]|[0-9]|]{1}[.|]?[0-9]*";

	// 数値かチェックする
	range = [string rangeOfString:expression options:NSRegularExpressionSearch];
	if (range.location != NSNotFound)
	{
		result = true; // 数値の場合
	}
	else
	{
		result = false; // 数値でない場合
	}

	// 確認するためにコンソールにログを出力する
	if (true == result)
	{
		NSLog(@"[O] %@ is number!!",string);
	}
	else
	{
		NSLog(@"[X] %@ is not number.",string);
	}

	return result;
}

テスト用の関数はこんな感じ。

int main (int argc, const char * argv[])
{
	// 変数
	Boolean result = false;

	// テスト1
	result = IsNumbers(@"");
	result = IsNumbers(@"+");
	result = IsNumbers(@"-");
	result = IsNumbers(@"*");
	result = IsNumbers(@".");

	// テスト2
	result = IsNumbers(@"1");
	result = IsNumbers(@"+1");
	result = IsNumbers(@"-1");
	result = IsNumbers(@"*1");

	// テスト3
	result = IsNumbers(@"0");
	result = IsNumbers(@"+0");
	result = IsNumbers(@"-0");
	result = IsNumbers(@"*0");

	// テスト4
	result = IsNumbers(@"0.");
	result = IsNumbers(@"+0.");
	result = IsNumbers(@"-0.");
	result = IsNumbers(@"*0.");

	// テスト5
	result = IsNumbers(@"1.");
	result = IsNumbers(@"+1.");
	result = IsNumbers(@"-1.");
	result = IsNumbers(@"*1.");

	// テスト6
	result = IsNumbers(@".0");
	result = IsNumbers(@"+.0");
	result = IsNumbers(@"-.0");
	result = IsNumbers(@"*.0");

	// テスト7
	result = IsNumbers(@".1");
	result = IsNumbers(@"+.1");
	result = IsNumbers(@"-.1");
	result = IsNumbers(@"*.1");

	// テスト8
	result = IsNumbers(@"0.0");
	result = IsNumbers(@"+0.0");
	result = IsNumbers(@"-0.0");
	result = IsNumbers(@"*0.0");

	// テスト9
	result = IsNumbers(@"0.1");
	result = IsNumbers(@"+0.1");
	result = IsNumbers(@"-0.1");
	result = IsNumbers(@"*0.1");

	// テスト10
	result = IsNumbers(@"1.0");
	result = IsNumbers(@"+1.0");
	result = IsNumbers(@"-1.0");
	result = IsNumbers(@"*1.0");

	// テスト11
	result = IsNumbers(@"1.1");
	result = IsNumbers(@"+1.1");
	result = IsNumbers(@"-1.1");
	result = IsNumbers(@"*1.1");

	// テスト12
	result = IsNumbers(@"1.10");
	result = IsNumbers(@"+1.10");
	result = IsNumbers(@"-1.10");
	result = IsNumbers(@"*1.10");

	// テスト13
	result = IsNumbers(@"1.01");
	result = IsNumbers(@"+1.01");
	result = IsNumbers(@"-1.01");
	result = IsNumbers(@"*1.01");

	// テスト14
	result = IsNumbers(@"01.1");
	result = IsNumbers(@"+01.1");
	result = IsNumbers(@"-01.1");
	result = IsNumbers(@"*01.1");

	return 0;
}

結果はこんな感じ。

[Switching to process 12445 thread 0x0]
2012-01-14 00:13:00.036 ProtoAnalysisString[12445:707] [X] is not number.
2012-01-14 00:13:00.088 ProtoAnalysisString[12445:707] [O] + is number!!
2012-01-14 00:13:00.089 ProtoAnalysisString[12445:707] [O] - is number!!
2012-01-14 00:13:00.090 ProtoAnalysisString[12445:707] [X] * is not number.
2012-01-14 00:13:00.090 ProtoAnalysisString[12445:707] [O] . is number!!
2012-01-14 00:13:00.091 ProtoAnalysisString[12445:707] [O] 1 is number!!
2012-01-14 00:13:00.092 ProtoAnalysisString[12445:707] [O] +1 is number!!
2012-01-14 00:13:00.092 ProtoAnalysisString[12445:707] [O] -1 is number!!
2012-01-14 00:13:00.093 ProtoAnalysisString[12445:707] [X] *1 is not number.
2012-01-14 00:13:00.094 ProtoAnalysisString[12445:707] [O] 0 is number!!
2012-01-14 00:13:00.094 ProtoAnalysisString[12445:707] [O] +0 is number!!
2012-01-14 00:13:00.095 ProtoAnalysisString[12445:707] [O] -0 is number!!
2012-01-14 00:13:00.096 ProtoAnalysisString[12445:707] [X] *0 is not number.
2012-01-14 00:13:00.096 ProtoAnalysisString[12445:707] [O] 0. is number!!
2012-01-14 00:13:00.097 ProtoAnalysisString[12445:707] [O] +0. is number!!
2012-01-14 00:13:00.098 ProtoAnalysisString[12445:707] [O] -0. is number!!
2012-01-14 00:13:00.098 ProtoAnalysisString[12445:707] [X] *0. is not number.
2012-01-14 00:13:00.099 ProtoAnalysisString[12445:707] [O] 1. is number!!
2012-01-14 00:13:00.100 ProtoAnalysisString[12445:707] [O] +1. is number!!
2012-01-14 00:13:00.100 ProtoAnalysisString[12445:707] [O] -1. is number!!
2012-01-14 00:13:00.102 ProtoAnalysisString[12445:707] [X] *1. is not number.
2012-01-14 00:13:00.103 ProtoAnalysisString[12445:707] [O] .0 is number!!
2012-01-14 00:13:00.104 ProtoAnalysisString[12445:707] [O] +.0 is number!!
2012-01-14 00:13:00.104 ProtoAnalysisString[12445:707] [O] -.0 is number!!
2012-01-14 00:13:00.105 ProtoAnalysisString[12445:707] [X] *.0 is not number.
2012-01-14 00:13:00.105 ProtoAnalysisString[12445:707] [O] .1 is number!!
2012-01-14 00:13:00.106 ProtoAnalysisString[12445:707] [O] +.1 is number!!
2012-01-14 00:13:00.107 ProtoAnalysisString[12445:707] [O] -.1 is number!!
2012-01-14 00:13:00.107 ProtoAnalysisString[12445:707] [X] *.1 is not number.
2012-01-14 00:13:00.108 ProtoAnalysisString[12445:707] [O] 0.0 is number!!
2012-01-14 00:13:00.109 ProtoAnalysisString[12445:707] [O] +0.0 is number!!
2012-01-14 00:13:00.110 ProtoAnalysisString[12445:707] [O] -0.0 is number!!
2012-01-14 00:13:00.111 ProtoAnalysisString[12445:707] [X] *0.0 is not number.
2012-01-14 00:13:00.111 ProtoAnalysisString[12445:707] [O] 0.1 is number!!
2012-01-14 00:13:00.112 ProtoAnalysisString[12445:707] [O] +0.1 is number!!
2012-01-14 00:13:00.113 ProtoAnalysisString[12445:707] [O] -0.1 is number!!
2012-01-14 00:13:00.113 ProtoAnalysisString[12445:707] [X] *0.1 is not number.
2012-01-14 00:13:00.114 ProtoAnalysisString[12445:707] [O] 1.0 is number!!
2012-01-14 00:13:00.115 ProtoAnalysisString[12445:707] [O] +1.0 is number!!
2012-01-14 00:13:00.116 ProtoAnalysisString[12445:707] [O] -1.0 is number!!
2012-01-14 00:13:00.116 ProtoAnalysisString[12445:707] [X] *1.0 is not number.
2012-01-14 00:13:00.117 ProtoAnalysisString[12445:707] [O] 1.1 is number!!
2012-01-14 00:13:00.118 ProtoAnalysisString[12445:707] [O] +1.1 is number!!
2012-01-14 00:13:00.118 ProtoAnalysisString[12445:707] [O] -1.1 is number!!
2012-01-14 00:13:00.119 ProtoAnalysisString[12445:707] [X] *1.1 is not number.
2012-01-14 00:13:00.120 ProtoAnalysisString[12445:707] [O] 1.10 is number!!
2012-01-14 00:13:00.120 ProtoAnalysisString[12445:707] [O] +1.10 is number!!
2012-01-14 00:13:00.121 ProtoAnalysisString[12445:707] [O] -1.10 is number!!
2012-01-14 00:13:00.122 ProtoAnalysisString[12445:707] [X] *1.10 is not number.
2012-01-14 00:13:00.123 ProtoAnalysisString[12445:707] [O] 1.01 is number!!
2012-01-14 00:13:00.123 ProtoAnalysisString[12445:707] [O] +1.01 is number!!
2012-01-14 00:13:00.124 ProtoAnalysisString[12445:707] [O] -1.01 is number!!
2012-01-14 00:13:00.125 ProtoAnalysisString[12445:707] [X] *1.01 is not number.
2012-01-14 00:13:00.126 ProtoAnalysisString[12445:707] [O] 01.1 is number!!
2012-01-14 00:13:00.127 ProtoAnalysisString[12445:707] [O] +01.1 is number!!
2012-01-14 00:13:00.128 ProtoAnalysisString[12445:707] [O] -01.1 is number!!
2012-01-14 00:13:00.128 ProtoAnalysisString[12445:707] [X] *01.1 is not number.
Program ended with exit code: 0

一応、判定できとるっぽいけど、正規表現部分はあまり自身がない…間違ってたらコメントでも何でもよいので教えてください。
あと、整数部分に3桁ごとにカンマがある数字は未対応…そのうち…がんばります。

んまま、メモってことで。

[amazonjs asin="4864010498″ locale="JP" title="iOSプログラミング 第2版"]