【问题标题】:iPhone , NSRegularExpression big floatiPhone , NSRegularExpression 大浮动
【发布时间】:2012-09-01 08:50:11
【问题描述】:

我有以下代码:

-(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 example", rhs: "44.5097254 Indian sample", error: "", icc: true}

我得到了结果44.5097254

但是对于这个字符串:

{lhs: "1 example", rhs: "44.5097254.00.124.2 sample", error: "", icc: true}

我得到44 的错误结果。我希望得到44.5097254.00.124.2

我做错了什么?

【问题讨论】:

    标签: iphone objective-c regex parsing nsregularexpression


    【解决方案1】:

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

    -(NSString*)getNumberFromString:(NSString*)theString
    {
        NSError *error = NULL;
        NSRegularExpression *regex =
        [NSRegularExpression regularExpressionWithPattern:@"rhs: \"(\\d+(?:\\.\\d+)*)"
                                                  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]];
    }
    
    ...
         NSLog(@"%@", [self getNumberFromString:
                       @"{lhs: \"1 example\", rhs: \"44.5097254.00.124.2 sample\", error: \"\", icc: true}"]);
    

    输出: 44.5097254.00.124.2

    【讨论】:

    • @user1466308 我刚刚运行了您发布的确切代码并将44.5097254.00.124.2 记录到控制台,使用@"rhs: \"(\\d+(?:\\.\\d+)*)" 作为表达式。
    • 问题是我可以读这个 44.3 但我看不懂 1.100000
    • @user1466308 我刚刚通过相同的代码运行它,我得到了正确的值,将我示例中的方法复制并粘贴到您的代码中,没有理由它不应该工作。
    【解决方案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]]);
    
    }
    

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

    【讨论】:

    • 没关系,自我研究是编程中最好的事情
    猜你喜欢
    • 2011-11-10
    • 2011-06-12
    • 2012-03-24
    • 2010-12-10
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多