Extract values between parenthesis from NSString

廉价感情. 提交于 2019-12-03 07:42:27

Just to be complete:

If you are absolutely sure of the format of your output you can use the array methods:

NSString *inputString; // this is in the form "xxxx(yyyy)"

NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:@"()"];
NSArray *splitString = [inputString componentsSeparatedByCharactersInSet:delimiters];

NSString *xString = [splitString objectAtIndex:0];
NSString *yString = [splitString objectAtIndex:1];

Of course, you need to be sure that the delimiting characters don’t exist in the inputString

Easiest way would be to use RegExKit:

http://regexkit.sourceforge.net/

Then you'd do something like:

[@"xxxx(yyyyy)" getCapturesWithRegexAndReferences:@"\\((.*)\\)",@"$1", &extractedString,nil];

and extractedString would contain whatever was in parenthesis.

Peter Hosey

Scan up to the ‘(’, then scan it, then scan up to the ')'. The result of the last scan is yyyy.

you might have a look at PKTokenizer in ParseKit:

http://parsekit.com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!