Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone,NSRegularExpression大浮动_Iphone_Objective C_Regex_Parsing_Nsregularexpression - Fatal编程技术网

iPhone,NSRegularExpression大浮动

iPhone,NSRegularExpression大浮动,iphone,objective-c,regex,parsing,nsregularexpression,Iphone,Objective C,Regex,Parsing,Nsregularexpression,我有以下代码: -(NSString*)getNumberFromString(NSString*)theString{ NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"rhs: \"([0-9]*[.]*[0-9]*)" options:NSRegularExpressionCaseInsensitive | NSReg

我有以下代码:

-(NSString*)getNumberFromString(NSString*)theString{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"rhs: \"([0-9]*[.]*[0-9]*)" options:NSRegularExpressionCaseInsensitive | NSRegularExpressionAnchorsMatchLines error:&error];
    NSArray *matches = [regex matchesInString:theString options:0 range:NSMakeRange(0, [theString length])];
    NSTextCheckingResult *match = [matches objectAtIndex:0];
    return [theString substringWithRange:[match rangeAtIndex:1]]);
}
当我尝试分析此字符串时:

{lhs:“1个示例”,rhs:“44.5097254印度样本”,错误:,icc:true}
我得到了结果
44.5097254

但是对于这个字符串:

{lhs:“1个示例”,rhs:“44.5097254.00.124.2示例”,错误:,icc:true}
我得到了不正确的
44
结果。我希望得到
44.5097254.00.124.2


我做错了什么?

您要查找的正则表达式是
rhs:\”(\d+(?:\。\d+*)


输出:
44.5097254.00.124.2

就像我在上一个问题中编辑的那样,在正则表达式的末尾添加一个*可以:

  -(NSString*)getNumberFromString(NSString*)theString{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"rhs: \"(([0-9]*[.]*[0-9]*)*)" options:NSRegularExpressionCaseInsensitive | NSRegularExpressionAnchorsMatchLines error:&error];
    NSArray *matches = [regex matchesInString:theString options:0 range:NSMakeRange(0, [theString length])];
    NSTextCheckingResult *match = [matches objectAtIndex:0];
    return [theString substringWithRange:[match rangeAtIndex:1]]);

}

对于未来,请尝试理解答案的作用。只需复制代码并认识到它不符合您的要求就可以了。但首先,您可以自己做一些研究。您可以很容易地看到,添加星号将使表达式适用于所有长度的值。

@user1466308我刚刚运行了您发布的确切代码,并且将
44.5097254.00.124.2
记录到控制台,使用
@“rhs:\”(\\d+(?:\\.\\d+)*)
作为表达式。问题是我可以读取这一个44.3,但无法读取1。100000@user1466308我只是用相同的代码运行了一下,得到了正确的值,将我的示例中的方法复制并粘贴到您的代码中,没有理由认为它不起作用
  -(NSString*)getNumberFromString(NSString*)theString{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"rhs: \"(([0-9]*[.]*[0-9]*)*)" options:NSRegularExpressionCaseInsensitive | NSRegularExpressionAnchorsMatchLines error:&error];
    NSArray *matches = [regex matchesInString:theString options:0 range:NSMakeRange(0, [theString length])];
    NSTextCheckingResult *match = [matches objectAtIndex:0];
    return [theString substringWithRange:[match rangeAtIndex:1]]);

}